0

我想使用下拉菜单更新我的数据库 我能找到的大多数 ajax 都是为了检索数据。有人可以帮忙吗。

我的 php updatestatus.php 页面是

include 'includes/session.php';
include 'includes/db_connection.php';
include 'includes/functions.php';

$status = $_POST['status'];
$id = $_POST['id'];
$sql = "UPDATE orders SET
        status = '$status'
        WHERE id = $id";

我在 order.php 中的选择框是

<select name="status" id="id" onchange="updateStatus((this.value),<?php echo $row['id']; ?>)">
   <option value="<?php echo $row['status']; ?>"><?php echo $row['status']; ?></option>
   <option value="Order Placed">Order Placed</option>
   <option value="Processing">Processing</option>
   <option value="Dispatched">Dispatched</option>
</select>

我在 order.php 中的 JavaScript 是

function updateStatus(status, id){
var url = "updatestatus.php";
if (window.XMLHttpRequest) { // branch for native XMLHttpRequest object
    req = new XMLHttpRequest();
    req.onreadystatechange = processReqChange;
    req.open('POST', url, true);
    req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    req.send(data);

}else if (window.ActiveXObject) { // branch for IE/Windows ActiveX version
    req = new ActiveXObject('Microsoft.XMLHTTP')
    if (req) {
        req.onreadystatechange = processReqChange;
        req.open('POST', url, true);
        req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        req.send(data);
    }
}
} 
4

2 回答 2

0

在行中:

 req.send(data);

在哪里data定义?它必须是包含您的id和的 url 编码字符串status这个问题在我搜索的时候出现了。

于 2013-01-04T04:33:07.657 回答
0

定义data为:

var data = "status="+status+"&id="+id;

还将您的sql更改为:

$sql = "UPDATE orders SET
        status = '".$status."'
        WHERE id =". $id;
于 2013-01-04T04:36:16.760 回答