$_POST为空,使用Ajax发送表单值

$_POST empty, sending form values with Ajax

本文关键字:表单 Ajax 使用 POST 为空      更新时间:2023-09-26

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);