我有一个以表格格式显示信息列表。现在我在此表中添加了一列,其中包含下拉列表,其中包含2个选项批准&拒绝。现在我想从下拉列表中选择选项时更新单个记录的状态,我想通过使用AJAX来实现。
PHP代码:
<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>
如果您有任何解决方案,请与我分享。
答案 0 :(得分:0)
function change_status(drop_id){
var status=document.getElementById(drop_id).value;
try
{
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}
catch (e)
{
// Internet Explorer
try
{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
try
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e)
{
alert("Your browser does not support AJAX!");
return false;
}
}
}
if(xmlHttp.readyState != 0){
xmlHttp.abort();
}
xmlHttp.onreadystatechange=function()
{
if(xmlHttp.readyState==4)
{
//Do something to notify the user that the status has been updated.
}
}
url="change_status.php?id="+drop_id+"&status="+status;
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
}
并且选择是这样的:
<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>
然后创建文件change_status.php 从$ _GET收到id和新状态的地方,你可以适当地更新数据库。
答案 1 :(得分:0)
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
<?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
?>
我认为这应该足以让你开始,这主要是在餐巾编码的背面写的,但我认为你会得到主要的想法。