数据在Jquery ajax帖子上插入两次 [英] data insert twice on Jquery ajax post
本文介绍了数据在Jquery ajax帖子上插入两次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我发布了带有jquery和php的ajax帖子的表单,但它在数据库中插入数据两次并显示它!
我的代码是
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>
<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代码
testin.php
PHP CODE
testin.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>';
推荐答案
( document )。ready( function (){
(document).ready(function() {
( #driver)。click( function (){
("#driver").click(function(){
.post(
testin.php,
这篇关于数据在Jquery ajax帖子上插入两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文