Click here to Skip to main content
15,867,594 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I have post form with ajax post with jquery and php but it inserts data twice in database and display it!!!


my code is

HTML
<html>
<head>
<title>the title</title>
  <script type="text/javascript" 
   src="JQuery/jquery-1.4.2.min.js"></script>
   <script>
  $(document).ready(function() {
      $("#driver").click(function(){
          $.post( 
             "testin.php",$("#frm").serialize(),
			 function(data) {
                $('#stage').html(data);
			}
          );
		     });
   });
   </script>
</head>
<body>
   <p>Click on the button to load result.html file:</p>
   <form id="frm" method="post"  önSubmit="return">
   <input type="text" id="firstname" name="firstname" >
   <input type="text" id="lastname" name="lastname">
    <div id="stage">
    </div>
   <input type="button" id="driver" value="Load Data" />
	</form>
</body>
</html>

PHP CODE
testin.php

PHP
$con = mysqli_connect("localhost","root");
mysqli_select_db($con,"ajaxdatabase");
$first = $_POST["firstname"];
$last = $_POST["lastname"];
$sql1 = "INSERT INTO user(FirstName,LastName)VALUES('$first','$last')";
$query1 = mysqli_query($con,$sql1);

if (!mysqli_query($con,$sql1))
  {
  die('Error: ' . mysqli_error($con));
  }
echo "1 record added";
$sql = "SELECT * FROM user";
$query = mysqli_query($con,$sql);

echo "<table border='1'>
<tr>
<th>Id</th>
<th>FirstName</th>
<th>LastName</th>
</tr>";
while ($result = mysqli_fetch_array($query))
{
	echo '<tr>';
	echo '<td>'.$result['Id'];
	echo '<td>'.$result['FirstName'];
	echo '<td>'.$result['LastName'];
	echo '</tr>';
		
}
echo'</table>';
Posted
Comments
Kornfeld Eliyahu Peter 8-Oct-13 5:35am    
Is it possible that you got two postback calls.
One from the jquery code and an other from the button click?
Md Jamaluddin Saiyed 8-Oct-13 12:23pm    
I have checked but I found nothing like that!

It was tricky!!!

You just running query twice (see my remarks)...

PHP
$con = mysqli_connect("localhost", "root");
mysqli_select_db($con, "ajaxdatabase");
$first = $_POST["firstname"];
$last = $_POST["lastname"];
$sql1 = "INSERT INTO user(FirstName,LastName)VALUES('$first','$last')";
$query1 = mysqli_query($con, $sql1); // FIRST RUN

if (!mysqli_query($con, $sql1)) { // SECOND RUN
    die('Error: ' . mysqli_error($con));
}
echo "1 record added";
$sql = "SELECT * FROM user";
$query = mysqli_query($con, $sql);

echo "<table border='1'>
<tr>
<th>Id</th>
<th>FirstName</th>
<th>LastName</th>
</tr>"
while ($result = mysqli_fetch_array($query)) {
    echo '<tr>';
    echo '<td>' . $result['Id'];
    echo '<td>' . $result['FirstName'];
    echo '<td>' . $result['LastName'];
    echo '</tr>';
}
echo'</table>';


If you want to check that the query went good do this:

PHP
if(mysql_affected_rows($conn)!=1) {
    die('Error: ' . mysqli_error($con));
}


Or you can drop the line remarked FIRST RUN...
 
Share this answer
 
v6
Comments
Md Jamaluddin Saiyed 9-Oct-13 2:42am    
Thanks a lot sir !! you solved my problem !! I could not found my mistake !!
Kornfeld Eliyahu Peter 9-Oct-13 2:43am    
You welcome!!!
You can use jQuery one method.It's like this:

C#
$("#driver").one("click", function () {
    alert("This will be displayed only once.");
});


I have written an article about this.Check that for more info:

How to Avoid Double Clicking With jQuery ?

UPDATE

<form id="frm" method="post"  önSubmit="return">


On above line remove your form post method and try again.

OR

PHP
$("#driver").one("click", function (event) {
          $.post("testin.php",$("#frm").serialize(),function(data) {
                $('#stage').html(data); });
             });

 event.preventDefault();
Return false;
});






I hope this will help to you.
 
Share this answer
 
v5
Comments
Md Jamaluddin Saiyed 8-Oct-13 12:17pm    
I have done it but it still inserts two records !!
Sampath Lokuge 9-Oct-13 2:14am    
Plz check my "UPDATE" section.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900