提问人:Alex 提问时间:4/18/2016 更新时间:11/12/2023 访问量:14836
从 Javascript onClick 更新数据库 By Ajax PHP
Update Database from Javascript onClick By Ajax PHP
问:
嗨,当用户单击下面的按钮时,我想更新id行,请帮忙,对不起我的英语不好
index.php
<!doctype html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<a href="javascript:void(0)" onClick="updateId('1')">Id 1</a>
<a href="javascript:void(0)" onClick="updateId('2')">Id 2</a>
<a href="javascript:void(0)" onClick="updateId('3')">Id 3</a>
</body>
</html>
update.php
<?php
include('database_connection.php');
$update = "UPDATE id SET id = id + 1 WHERE id = updateId";
if (mysqli_query($connect, $update)) {
echo "Record updated successfully";
} else {
echo "Error updating record: " . mysqli_error($connect);
}
?>
答:
2赞
Manjeet Barnala
4/18/2016
#1
在您的 index.php 文件中
<!doctype html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<a href="javascript:void(0)" onClick="updateId('1')">Id 1</a>
<a href="javascript:void(0)" onClick="updateId('2')">Id 2</a>
<a href="javascript:void(0)" onClick="updateId('3')">Id 3</a>
</body>
</html>
<script>
function updateId(id)
{
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
{
alert(xmlhttp.responseText);
}
};
xmlhttp.open("GET", "update.php?id=" +id, true);
xmlhttp.send();
}
</script>
在你的update.php
<?php
if(!empty($_GET['id']))
{
$id = $_GET['id'];
include('database_connection.php');
$update = "UPDATE id SET id = id + 1 WHERE id = ?";
$connect->execute_query($update, [$id]);
}
评论
0赞
Manjeet Barnala
4/18/2016
@Doraemon欢迎亲爱的,很高兴帮助你...如果对您有帮助,请投票我的答案......
0赞
Alex
4/18/2016
每当它更新 id 时,我都会有一个问题,它会发出警报。我怎样才能阻止这种情况。
0赞
Manjeet Barnala
4/18/2016
在 Ajax 调用中删除此行...,这是对 Ajax 调用响应的警报。.alert(xmlhttp.responseText);
评论