使用AJAX/PHP/MySQL批准/拒绝记录
我有一个表格格式的信息列表。现在我在这个表中添加了一列,它有两个选项approve&reject的下拉列表。现在,当他从下拉列表中选择选项时,我想更新单个记录的状态,我想使用AJAX进行更新 PHP代码:使用AJAX/PHP/MySQL批准/拒绝记录,php,mysql,ajax,Php,Mysql,Ajax,我有一个表格格式的信息列表。现在我在这个表中添加了一列,它有两个选项approve&reject的下拉列表。现在,当他从下拉列表中选择选项时,我想更新单个记录的状态,我想使用AJAX进行更新 PHP代码: <form name="post_action" method="post" action="" id="post_action"> <select name="post_status" id="<?=$row['id']; ?>" style="width:17
<form name="post_action" method="post" action="" id="post_action">
<select name="post_status" id="<?=$row['id']; ?>" style="width:175px; float:left; height:25px;" >
<option selected="selected" disabled="disabled">--Select--</option>
<option value="1">Approved</option>
<option value="2">Rejected</option>
<option value="3">Pending</option>
</select>
选择如下:
<select name="post_status" id="<?=$row['id']; ?>" onchange="change_status(<?=$row['id']; ?>)" style="width:175px; float:left; height:25px;" >
<option selected="selected" disabled="disabled">--Select--</option>
<option value="1">Approved</option>
<option value="2">Rejected</option>
<option value="3">Pending</option>
</select>
JQuery
var serializedData = $form.serialize();
$("input[type=select]").change(function() {
$.ajax({//Submit the value of the drop_down
url: "/updateDB.php",
type: "post",
data: serializedData
}).done(function() {
alert('Updated!');
});
});
PHP
javascript/jquery必须触发onchange
事件,然后使用AJAX将其发送到PHP控制器/文件以更新数据库记录。
var serializedData = $form.serialize();
$("input[type=select]").change(function() {
$.ajax({//Submit the value of the drop_down
url: "/updateDB.php",
type: "post",
data: serializedData
}).done(function() {
alert('Updated!');
});
});
<?php
if(isset($_POST['id']){
$id = $_POST['id'];
$post_status = $_POST['post_status'];
$sql = "UPDATE mytable SET myvalue = '$post_status' WHERE id = $id";
//Connect to database
$link = mysqli_connect("myhost","myuser","mypassw","mydb") or die("Error " . mysqli_error($link));
//Execute the Query
$result = $link->query($sql);
//Using the $result object you can determine if the update was successful or not and alert the user of success/failure
?>