-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);
        }
    );
});
1
  • will you please tell me how to clear it i am new in web. Commented Jun 1, 2016 at 16:23

2 Answers 2

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
        }
    );
});
Sign up to request clarification or add additional context in comments.

8 Comments

when i use it page reload
It must have been doing that before
without reload i want it clear
i am using jquery because i just dont want to reload my page
See amended answer
|
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.

4 Comments

yes i have use this but by using this page reload
you must add "e.preventDefault();"
$( document ).ready(function() { $('#messageForm').submit(function(e){ e.preventDefault(); }); $('#send').click(function(e){ $("#messageForm")[0].reset() $.post( $('#messageForm').attr('action'), $('#messageForm :input').serializeArray(), function(result){ $('#userschat').html(result); } ); }); });
i am doing like this but its not working

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.