使用ajax更新数据库

时间:2013-01-04 04:20:27

标签: php jquery ajax

我想使用下拉菜单更新我的数据库 我能找到的大多数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);
    }
}
} 

2 个答案:

答案 0 :(得分:0)

在行中:

 req.send(data);

在哪里定义data?它必须是包含您的idstatus的网址编码字符串。我搜索时This question出现了。

答案 1 :(得分:0)

data定义为:

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

还要将您的sql更改为:

$sql = "UPDATE orders SET
        status = '".$status."'
        WHERE id =". $id;