-5

I would like to use JQuery to submit my form without reloading the page. How can I do this?

My Form:

<html>
<form method="post" action="submit.php">
    <input type="text" name="name"/>
    <input type="submit" name="submit" value="submit"/>
</form>
</html>

My PHP:

<?php
    $name= $_POST['name'];
        echo $name;
?>

3 Answers3

2

As per comments, please refer to the jQuery docs/1000's of tutorials on the matter. Nonetheless:

$('#form-id').submit(function(){

// work yon javascript magicke

 });

Edit: do note this is a non ajax solution for a simple form submit (client side-validation etc.); if you want to get fancy refer to the other answers.

Nick Tomlin
  • 28,402
  • 11
  • 61
  • 90
1

By using ajax :

$('form').on('submit', function(e) {
    e.preventDefault();
    $.ajax({
       type: 'POST',
       url : 'submit.php',
       data: $(this).serialize()
    });
});
adeneo
  • 312,895
  • 29
  • 395
  • 388
0

You will need to wait until the document has loaded, then interupt the submit event, and run some ajax

$( document ).ready( function () {

    $( 'form' ).on( 'submit', function ( event ) {

        event.preventDefault();

        $.ajax({
            type: 'POST',
            url: '/mypage.php',
            data: $( this ).serialize(),
            success: function ( data ) {
                alert( data );
            }
        });

    }):

});
whitneyit
  • 1,226
  • 8
  • 17