HTML
Client:
Total:
PHP(insert.php)
echo file_get_contents('php://input');
include_once "connect.php";
if ($db_found){
if (isset($_POST['submitted'])) {
foreach($_POST AS $key => $value) {
$_POST[$key] = mysql_real_escape_string($value);
}
$sql = "INSERT INTO `mytable` ( `idclient` , `total` , ) " .
"VALUES( {$_POST['idclient']} , {$_POST['total']} ) ";
mysql_query($sql) or die(mysql_error());
}
}
mysql_close($db_handle);
?>
这可行,但是当我尝试使用Ajax调用插件时,$ _POST函数为空,我无法访问表单中的值。
这是ajax代码和函数调用:
的Ajax
function Save()
{
xmlHttp = getXMLHttp(); // returns a new XMLHttpRequest or ActiveXObject
xmlHttp.onreadystatechange = function(){
if(xmlHttp.readyState == 4) {
document.getElementById("btnSave").value = "Saved";
document.getElementById("result").innerHTML = xmlHttp.responseText;
}
else{
document.getElementById("btnSave").value = "Saving...";
}
}
xmlHttp.open("POST", "insert.php", true);
xmlHttp.send(null); // Do I need to create and pass a parameter string here?
}
执行echo file_get_contents('php://input');确实$ _POST为空,参数值不会传递。
我可以像这样连接URL中的params值
xmlHttp.open("POST", "insert.php?idclient=123&total=43", true);
但是,有没有办法使用$ _POST并利用它?