2

Is this code correct? I'm trying to submit it and also I would like on submit if text area would be empty again.

<script type="text/javascript">
$(document).ready(function(){

    $("form#submit").submit(function() {
        // we want to store the values from the form input box, then send via ajax below
        var fid = $(".messag").attr("id");
        var val = $("#mess_ar").val();
        $.ajax({
            type: "POST",
            url: "send_message.php",
            data: "fid="+ fid +"&val="+ val,
            success: function(){
                    $("#mess_ar").
            }
        });
        return false;
    });
}):
</script>

I'm trying to upload this:

<form id="submit" method="POST">
<textarea name="mess_cont" id="mess_ar" autofocus="autofocus" cols="70" rows="5">      </textarea>
<button id="mess_but" type="submit">Send</button>
</form>

Thankx...

6
  • you probably need to add a parameter into your event catching function like so $("form#submit").submit(function(e) { you can then use e.preventDefault() to stop the default action, submit, from happening
    – ianbarker
    Commented Mar 20, 2012 at 14:53
  • @ianbarker return false; is already being used at the end. Commented Mar 20, 2012 at 14:54
  • Nothing in the HTML you posted has class "messag", so that line that tries to set "fid" will set it to null. Also the "success" function is clearly incomplete; do you want to just cal .val('') to clear the textarea?
    – Pointy
    Commented Mar 20, 2012 at 14:54
  • @TJ. I missed that, however I think preventDefault() is the preferred method when using jQuery
    – ianbarker
    Commented Mar 20, 2012 at 14:57
  • .messag is element out of this form
    – Jakub Zak
    Commented Mar 20, 2012 at 14:57

3 Answers 3

3

Looks good. To empty the textarea use the following in the ajax success callback:

$("#mess_ar").val('');
2
​$(function(){
    $("form#submit").submit(function(e){
        e.preventDefault();
        var fid = $(".messag").attr("id");
        var val = $("#mess_ar").val();
        $.ajax({
            type: "POST",
            url: "send_message.php",
            data: "fid="+ fid +"&val="+ val,
            success: function(){
                $("#mess_ar").val("");
            }
        });
    });
});​

Use

$("#submit").on('submit', function(e){...});

instead of

$("#submit").submit(function(e){...});

if you are using latest version of jquery.

2
  • why is it on submit reloading whole page?
    – Jakub Zak
    Commented Mar 20, 2012 at 15:07
  • 1
    Did you added e.preventDefault(), please take a look at this jsfiddle.net/GkCbD/2
    – The Alpha
    Commented Mar 20, 2012 at 15:11
2

What are you actually looking for? Does it return the data in response too? Add the functions to track your the error case too. Make something like

<script type="text/javascript">
$(document).ready(function(){

    $("form#submit").submit(function() {
       // we want to store the values from the form input box, then send via ajax below
       var fid = $(".messag").attr("id");
       var val = $("#mess_ar").val();
       $.ajax({
          type: "POST",
          url: "send_message.php",
          data: "fid="+ fid +"&val="+ val,
          success: function(incoming_data){
             // ALERT incoming data if coming
             $("#mess_ar").text(""); // DO YOUR JOB CONTINUOU
          },
          error: function() { 
             alert("BROKEN REQUEST.");
          }
       });
       return false;
   });

});
</script>

Else, seems all fine.

2
  • and I should be able to receive them in my php file line normal $_POST['']?
    – Jakub Zak
    Commented Mar 20, 2012 at 15:00
  • yeah, as you are passing parameters from your ajax ` data: "fid="+ fid +"&val="+ val` so they can be accessed in php by $_POST['fid'] and '$_POST['val']'. However, error comes in the form of client side error object. So, if you want to pass to php server side, you need to make another can in which you pass your Error Object! Commented Mar 20, 2012 at 15:06

Not the answer you're looking for? Browse other questions tagged or ask your own question.