我在ajax中有以下功能:
function update_row(ID,CODE,NAME,PRICE){
$.ajax({
url: "update-record.php",
data: "ID="+ID,
date: "CODE="+CODE,
data: "NAME="+NAME,
data: "PRICE="+PRICE,
type: "post",
success: function(result){
$('#prod2_'+ID).hide();
}
});
}
这应该调用" update-record.php"这是以下内容:
<?php
include "config.php";
$product_id = $_POST['ID'];
$product_code = $_POST['CODE'];
$product_name = $_POST['NAME'];
$product_price = $_POST['PRICE'];
$sql = mysql_query("UPDATE `products` SET `code`=$product_code,`name`=$product_name,`price`=$product_price WHERE 'id'=$product_id");
echo json_encode('Record deleted');
?>
这是我用来调用ajax函数的按钮:
'<div><input id=prod2_"'.$row['ID'].'" type=button name="click3" onclick="update_row('.$row['ID'].','.$row['code'].','.$row['name'].','.$row['price'].')" value="Hide"></div>';
此按钮应更新数据库记录并隐藏&#34;表格&#34;我发布数据的地方。不幸的是它什么也没做。
有任何建议如何解决问题? 谢谢你的帮助!
答案 0 :(得分:1)
更改此
'<div><input id="prod2_'.$row['ID'].'" type="button" name="click3" onclick="update_row(\''.$row['ID'].'\',\''.$row['code'].'\',\''.$row['name'].'\',\''.$row['price'].'\')" hidden="yes"></div>';
并更改此
您需要连接mysql服务器
$dbhost = 'localhost:3036';
$dbuser = 'root';
$dbpass = 'rootpassword';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
并连接到查询
mysql_query($conn,"UPDATE products SET code='$product_code',name='$product_name',price='$product_price' WHERE id='$product_id'");
并更改此
function update_row(ID,CODE,NAME,PRICE){
var dataString = 'ID='+ ID + '&CODE=' + CODE + '&NAME=' + NAME + '&PRICE=' + PRICE;
$.ajax({
url: "rt.php",//Your php page code this is my test code
data: dataString,
type: "post",
success: function(result){
$('#prod2_'+ID).hide();
}
});
}
答案 1 :(得分:0)
您的ajax数据对象有误。请试试这个:
function update_row(ID, CODE, NAME, PRICE) {
$.ajax({
url: "update-record.php",
data: "ID=" + ID+"&CODE=" + CODE+"&NAME=" + NAME + "&PRICE=" + PRICE,
type: "post",
success: function(result) {
$('#prod2_' + ID).hide();
}
});
}
答案 2 :(得分:0)
<link rel="stylesheet" type="text/css" href="css.css">
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>
<meta charset="UTF-8">
<script>
function delete_row(ID){
$.ajax({
url: "remove-record.php",
data: "ID="+ID,
type: "post",
success: function(result){
$('#prod_'+ID).remove();
}
});
}
function show_row(ID){
$.ajax({
data: "ID="+ID,
type: "post",
success: function(result){
$('#prod2_'+ID).show();
}
});
}
function update_row(ID,CODE,NAME,PRICE){
var dataString = 'ID='+ ID + '&CODE=' + CODE + '&NAME=' + NAME + '&PRICE=' + PRICE;
$.ajax({
url: "update-record.php",
data: dataString,
type: "post",
success: function(result){
$('#prod2_'+ID).hide();
}
});
}
</script>
<title></title>
</head>
<body>
<div class="tittle"><b>Delete product</b></div>
<?php include "menu.html"; ?>
<div style = "margin:30px" align = "center">
<form action="deleteproduct.php" method="post" >
<fieldset>
<?php
include 'config.php';
$aktiv=$_COOKIE['account_id'];
echo "<ul id='productlist'>
<li>
<div>Code</div>
<div>Name</div>
<div>Price</div>
</li>";
$sql = "SELECT * FROM products where updated_account_ID='$aktiv' order by ID";
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result)) {
echo "<li id=prod_".$row['ID'].">";
echo '<div>' . $row['code'] . '</div>';
echo '<div>' . $row['name'] . '</div>';
echo '<div>' . $row['price'] . '</div>';
echo '<div><input type=button name="click" onclick="delete_row('.$row['ID'].')" value="Delete"></div>';
echo '<div><input type=button name="click2" onclick="show_row('.$row['ID'].')" value="Update"></div>';
echo "<li hidden id=prod2_".$row['ID'].">";
echo '<div><input type="text" value="'.$row['code'].'"</div>';
echo '<div><input type="text" value="'.$row['name'].'"</div>';
echo '<div><input type="text" value="'.$row['price'].'"</div>';
echo '<div><input type="button" name="click3" onclick="update_row(\''.$row['ID'].'\',\''.$row['code'].'\',\''.$row['name'].'\',\''.$row['price'].'\')" value="Save"></div>';
echo '</li>';
echo '</li>';
}
echo "</ul>";
?>
</fieldset>
</form>
</div>
</body>
</html>