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 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? Commented Mar 20, 2012 at 14:54
  • @TJ. I missed that, however I think preventDefault() is the preferred method when using jQuery Commented Mar 20, 2012 at 14:57
  • .messag is element out of this form 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('');
Sign up to request clarification or add additional context in comments.

Comments

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 Comments

why is it on submit reloading whole page?
Did you added e.preventDefault(), please take a look at this jsfiddle.net/GkCbD/2
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 Comments

and I should be able to receive them in my php file line normal $_POST['']?
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!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.