HTML
<form action='insert.php' method='POST'>
<p><b>Client:</b><input type='text' name='idclient'/>
<p><b>Total:</b><br /><input type='text' name='total'/>
<p><input type='submit' value='Save' id="btnSave"/>
<input type='hidden' value='1' name='submitted' />
</form>
PHP (insert.php)
<?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 и вызов функции:
<form action="javascript:Save()" method='POST'>
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пусто и значения параметров не передаются.
Я мог бы объединить значение params в URL-адресе, как это
xmlHttp.open("POST", "insert.php?idclient=123&total=43", true);
Но есть ли способ использовать $ _POST и воспользоватьсяиз этого?