Skip to content Skip to sidebar Skip to footer

Submitting A Form Without Refreshing The Page

I'm having a problem with jQuery(or maybe php). I want forms to be submitted without refreshing the page so i ended up with the following code but the problem is it doesn't work. H

Solution 1:

i dont know why its not working but you can try like this

<form id="myForm" name="myForm" action="">
  <p>
    <label for="name"></label>
    <input type="text" name="name"id="name" />
  </p>  
  <p>
     <input type="button" name="submit"id="submit" value="Submit" />//change type to button
  </p>
</form>

also make an error callback

$(function(){
 $("#submit").click(function(e) {
                e.preventDefault();
                var name = $("#name").val();
                $.ajax({
                type: "POST",
                url: "p.php",
                data: {name:name},
                success: function(){
                    alert("It works");
                },
                error:function(e){alert("it failed");}
                });

            });   

});

Solution 2:

You need to modify your form tag like this:

<form id="myForm" name="myForm" action="" onsubmit="return false;">

Post a Comment for "Submitting A Form Without Refreshing The Page"