rdkurth Posted December 10, 2013 Share Posted December 10, 2013 The three scripts below are working except when it returns "Successfully Inserted" it creates a new page. It is suppose to return "Successfully Inserted" between the <span> tags in the index.php page index.php <form id="myForm" action="userInfo.php" method="post">Name: <input type="text" name="name" /><br />Age : <input type="text" name="age" /><br /><button id="sub">Save</button></form> <span id="result"></span> my_script.js $("#sub").click( function() { $.post( $("#myForm").attr("action"), $("#myForm :input").serializeArray(), function(info){ $("#result").html(info); }); clearInput();}); $("#myForm").submit( function() { return false; }); function clearInput() { $("#myForm :input").each( function() { $(this).val(''); });} userinfo.php include_once('db.php'); $name = $_POST['name']; $age = $_POST['age']; if(mysql_query("INSERT INTO user VALUES('$name', '$age')")) echo "Successfully Inserted"; else echo "Insertion Failed"; Link to comment https://forums.phpfreaks.com/topic/284672-my-ajax-script-will-not-return-data-to-the-page-that-was-not-suppose-to-refresh/ Share on other sites More sharing options...
gristoi Posted December 10, 2013 Share Posted December 10, 2013 if your using ajax to submit the form then you need to prevent the forms native submission, otherwise the page will reload: $("#sub").click( function() { $.post( $("#myForm").attr("action"), should be $("#sub").click( function(e) { //stop the click event from bubbling e.preventDefault(); $.post( $("#myForm").attr("action"),.....); .............. return false; // at end } Link to comment https://forums.phpfreaks.com/topic/284672-my-ajax-script-will-not-return-data-to-the-page-that-was-not-suppose-to-refresh/#findComment-1461979 Share on other sites More sharing options...
rdkurth Posted December 10, 2013 Author Share Posted December 10, 2013 if your using ajax to submit the form then you need to prevent the forms native submission, otherwise the page will reload: $("#sub").click( function() { $.post( $("#myForm").attr("action"), should be $("#sub").click( function(e) { //stop the click event from bubbling e.preventDefault(); $.post( $("#myForm").attr("action"),.....); .............. return false; // at end } I changed it but it is still make page reload any other ideas $("#sub").click( function(e) { //stop the click event from bubbling e.preventDefault(); $.post( $("#myForm").attr("action"), $("#myForm :input").serializeArray(), function(info){ $("#result").html(info); return false; // at end }); clearInput(); }); $("#myForm").submit( function() { return false; }); function clearInput() { $("#myForm :input").each( function() { $(this).val(''); }); } Link to comment https://forums.phpfreaks.com/topic/284672-my-ajax-script-will-not-return-data-to-the-page-that-was-not-suppose-to-refresh/#findComment-1462004 Share on other sites More sharing options...
AJinNYC Posted December 11, 2013 Share Posted December 11, 2013 Just a shot in the dark but you are pulling in the jQuery library, right? I'm assuming so, but I don't see it in the code posted, so thought I'd check. Link to comment https://forums.phpfreaks.com/topic/284672-my-ajax-script-will-not-return-data-to-the-page-that-was-not-suppose-to-refresh/#findComment-1462053 Share on other sites More sharing options...
rdkurth Posted December 11, 2013 Author Share Posted December 11, 2013 plunged it into full on production form and it worked don't no why it would not work with this little test form Thanks for the help Link to comment https://forums.phpfreaks.com/topic/284672-my-ajax-script-will-not-return-data-to-the-page-that-was-not-suppose-to-refresh/#findComment-1462107 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.