-1

i have a little problem in form submit when i click send . message send but message box not clear i am using jquery to submit the form . i just want when i click on send button my message field clear auto . how to made it? here is my form html

    <form id ="messageForm" action = "<?php $_PHP_SELF ?>" method = "POST">
    <input type="hidden" name="sender_email" id="sender_email" value="">
    <input type="hidden" name="reciver_email" id="reciver_email" value="">
<input type="text" name="data" id="data"   placeholder="Message..." required=""   />
    <button id='send'>Send</button>
</form>

this is my jquery code

   $('#messageForm').submit(function(){
    return false;
});

$('#send').click(function(){

    $.post(     
        $('#messageForm').attr('action'),
        $('#messageForm :input').serializeArray(),
        function(result){
            $('#userschat').html(result);
        }
    );
});
Mike
  • 23,542
  • 14
  • 76
  • 87
faisal iqbal
  • 724
  • 1
  • 8
  • 20

2 Answers2

1

Then you have to clear that field manually, at some point in your code that make logical sense, I suggest from within the function in the $.post

Also you need to use preventDefault() to stop the form refreshing the page

$('#send').click(function(e){         //<- amended
    e.preventDefault();               //<- new line
    $.post(     
        $('#messageForm').attr('action'),
        $('#messageForm :input').serializeArray(),
        function(result){
            $('#userschat').html(result);
            $('#data').val('');                        //<- new line
        }
    );
});
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
0

I've tested this cose and it is correct:

  $( document ).ready(function() {
    $('#messageForm').submit(function(e){
       e.preventDefault();
    });
    $('#send').click(function(e){
      $("#messageForm")[0].reset()
    });
  });

Your javascript must be included in $( document ).ready and it must be completed with your logic.

DeepCode
  • 358
  • 2
  • 9