Jump to content

My ajax script will not return data to the page that was not suppose to refresh


rdkurth
Go to solution Solved by AJinNYC,

Recommended Posts

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
Share on other sites

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
Share on other sites

 

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.