-3

i have a small form chat like this and i want when click submit that form will not make the page refresh.

<form name="form1" method="post" id="formchat" action="forum_add_1313940.xhtml">
<input name="text" id="text2" max-lenght="1000"></input>
<input type="submit" name="submit" class="submit" value="Gửi" id="chats" />
</form>

and the js

<script language="JavaScript">
$('#formchat').submit(function () 
{
  sendformchat(); return false; 
});
</script>

But when i click "Gửi", the page gonna refresh. How to make it when click "Gửi", the page not gonna refresh ?

1 Answers1

0

If you want asynchronous behavior, you should handle the form in an AJAX style, you can read about AJAX here!

here is an example using jquery on your form:

var form = $('#form_chat');

form.find('input[type=submit]').on('click', function(e) {
    e.prevetDefault();
    $.ajax({
      type: "POST",
      url: form.attr('action'),
      data: form.serialize(),
      success: function(response) {
        console.log(response);
      }
    });
});
thislooksfun
  • 896
  • 9
  • 23
Amir
  • 1,951
  • 1
  • 20
  • 31