$_POST为空,使用Ajax发送表单值
$_POST empty, sending form values with Ajax
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调用insert时,$_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实际上是空的,并且不传递参数值。
我可以像这个一样在URL中连接params值
xmlHttp.open("POST", "insert.php?idclient=123&total=43", true);
但是,有没有办法使用$_POST并利用它?
您需要将内容类型设置为application/x-www-form-urlencoded
,以便在$_POST
中显示值。
例如xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
(如果使用XMLHttpRequest
对象(。
根据这些答案,我得出了以下结论:
var xmlHttp = getXMLHttp(); // returns a new XMLHttpRequest or ActiveXObject
function Save() {
if(xmlhttp) {
xmlhttp.open("POST","insert.php",true);
xmlhttp.onreadystatechange = handleServerResponse;
xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xmlhttp.send("idclient=" + document.getElementById("idclient").value
+ "&total=" + document.getElementById("total").value);
}
}
function handleServerResponse() {
if (xmlhttp.readyState == 4) {
if(xmlhttp.status == 200) {
alert("responseText= " + xmlhttp.responseText);
document.getElementById("btnSave").value = "Saved";
document.getElementById("result").innerHTML = xmlhttp.responseText;
}
else {
alert("Error during AJAX call. Please try again");
}
}
else{
document.getElementById("btnSave").value = "Saving...";
}
}
经过一段时间的搜索,我发现将这个函数定义为单独的(而不是像最初的问题中那样在onreadystatechange中内联(会使它发挥作用。
xmlhttp.onreadystatechange = handleServerResponse;
然而,@Daren在"特殊"输入值上提出了一个很好的观点。例如,传递一个"&"字符串字段中的字符将得到不完整的值。
请为您的表单尝试以下操作:
<form action="javascript:Save();return false;" method='POST'>
<p><b>Client:</b><input id='client' type='text' name='idclient'/>
<p><b>Total:</b><br /><input id='total' type='text' name='total'/>
<p><input type='submit' value='Save' id="btnSave"/>
<input type='hidden' value='1' name='submitted' />
</form>
然后这样打电话:
xmlHttp.open("POST", "insert.php", true);
var postStr = "idclient=" + document.getElementById("client").value + "&total=" + document.getElementById("total").value;
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlHttp.send(postStr);
相关文章:
- Ajax发布表单序列化,发布引号'
- JS验证ajax返回的html中的表单数据
- 将图像上传ajax与表单提交ajax相结合
- 使用ajax将数据从一个步骤发送到下一个步骤的3步表单
- 表单不会通过ajax提交
- Django-提交表单Ajax(替换Div)
- 在wordpress中添加带有短代码的联系表单ajax
- 成功后如何提交表单ajax帖子
- 同一页面上的多个表单AJAX
- 表单AJAX发送内部的twitter引导模式(Symfony2)
- 在Javascript中获取重力表单AJAX确认消息而不是输出
- HTML提交表单ajax响应
- Javascript表单AJAX样式
- 动态填充的Jquery表单;Ajax
- 对提交表单ajax帖子进行验证
- fanybox表单ajax post关闭提交
- jQuery表单:Ajax响应被显示,但是源代码中没有显示
- 重新加载提交表单Ajax调用
- 在jQuery上正常重新发送HTML表单ajax失败
- fanybox表单ajax提交,只有一个ajax请求