我的数据库中有订单表,它有id,name和status columns。然后我有html表代表我的订单表和里面我有 我希望使用ajax更新状态列的按钮,使其异步显示html表格中的更改。我怎么能 实现这个?我试过运行ajax函数onclick一个按钮传递order id作为参数然后使用get方法获取结果但是 document.getElementsByClassName方法有问题,它没有在html表中填充我的状态字段,也没有更改数据库。
orders.php
$query=mysqli_query($conn,"SELECT*FROM orders");
echo "<table border='1'>";
echo "<th>Id</th><th>Name</th><th>Status</th><th>Considering</th><th>Accepted</th>";
while($row=mysqli_fetch_array($query)){
$id=$row['id'];
$name=$row['name'];
$status=$row['status'];
echo "<tr>
<td>$id</td><td>$name</td><td><div class='status'>$status</div></td><td><buttton onclick=\"considering('$id')\">Considering</button></td>
<td><buttton onclick=\"accepted('$id')\">Accepted</button></td>
</tr>";
}
admin.php的
<?php
include "include/connect.php";
?>
<!DOCTYPE html>
<html>
<head>
<script>
function considering(one){
var a=new XMLHttpRequest();
a.open("GET","considering_parser.php?one="+one,true);
a.onreadystatechange=function(){
if(a.readyState==4 && a.status==200){
var returndata=a.responseText;
var x=document.getElementsByClassName("status");
for(var i=0;i<x.length;i++){
x[i].innerHTML=returndata;
}
}
}
a.send(null);
}
function accepted(two){
var b=new XMLHttpRequest();
b.open("GET","accepted_parser.php?two="+two,true);
b.onreadystatechange=function(){
if(b.readyState==4 && b.status==200){
var returndata=b.responseText;
var x=document.getElementsByClassName("status");
for(var i=0;i<x.length;i++){
x[i].innerHTML=returndata;
}
}
}
b.send(null);
}
</script>
</head>
<body>
<div id="content">
<?php
include "orders.php";
?>
</div>
</body>
</html>
considering_parser.php
<?php
include "include/connect.php";
$id=$_GET['one'];
$query=mysqli_query($conn,"UPDATE orders SET status=1 WHERE id='$id'");
$query=mysqli_query($conn,"SELECT * FROM orders WHERE id='$id'");
while($row=mysqli_fetch_array($query)){
$one=$row['status'];
echo $one;
}
?>
accepted_parser.php
<?php
include "include/connect.php";
$id=$_GET['two'];
$query=mysqli_query($conn,"UPDATE orders SET status=2 WHERE id='$id'");
$query=mysqli_query($conn,"SELECT * FROM orders WHERE id='$id'");
while($row=mysqli_fetch_array($query)){
$two=$row['status'];
echo $two;
}
?>
帮助将不胜感激
答案 0 :(得分:0)
好的,我解决了,感谢Fred-ii建议使用multi_query()
orders.php
$query=mysqli_query($conn,"SELECT*FROM orders");
echo "<table border='1'>";
echo "<th>Id</th><th>Name</th><th>Status</th><th>Considering</th><th>Accepted</th>";
while($row=mysqli_fetch_array($query)){
$id=$row['id'];
$name=$row['name'];
$status=$row['status'];
echo "<tr>
<td>$id</td><td>$name</td><td><div class='status$id'>$status</div></td><td><buttton onclick=\"considering('$id')\">Considering</button></td>
<td><buttton onclick=\"accepted('$id')\">Accepted</button></td>
</tr>";
}
admin.php的
<html>
<head>
<script>
function considering(one){
var a=new XMLHttpRequest();
a.open("GET","considering_parser.php?one="+one,true);
a.onreadystatechange=function(){
if(a.readyState==4 && a.status==200){
var returndata=a.responseText;
var x=document.getElementsByClassName("status"+one);
for(var i=0;i<x.length;i++){
x[i].innerHTML=returndata;
}
}
}
a.send(null);
}
function accepted(two){
var b=new XMLHttpRequest();
b.open("GET","accepted_parser.php?two="+two,true);
b.onreadystatechange=function(){
if(b.readyState==4 && b.status==200){
var returndata=b.responseText;
var x=document.getElementsByClassName("status"+two);
for(var i=0;i<x.length;i++){
x[i].innerHTML=returndata;
}
}
}
b.send(null);
}
</script>
</head>
<body>
<div id="content">
<?php
include "orders.php";
?>
</div>
</body>
</html>
considering_parser.php
<?php
include "include/connect.php";
$id=$_GET['jedan'];
$query="UPDATE orders SET status=1 WHERE id='$id';
SELECT * FROM orders WHERE id='$id';";
if (mysqli_multi_query($conn,$query))
{
do
{
// Store first result set
if ($result=mysqli_store_result($conn))
{
while ($row=mysqli_fetch_array($result))
{
$one=$row['status'];
echo $one;
}
}
}
while (mysqli_more_results($conn) && mysqli_next_result($conn));
}
mysqli_close($conn);
?>
accepted_parser.php的相似代码。感谢Fred-ii