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

时间:2011-07-01 17:43:44

标签: php javascript mysql 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调用插件时,$ _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并利用它?

4 个答案:

答案 0 :(得分:3)

您需要将内容类型设置为application/x-www-form-urlencoded,以便在$_POST中显示值。

例如xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");(如果使用XMLHttpRequest个对象)。

答案 1 :(得分:1)

根据答案,这就是我想出的:

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在“特殊”输入值上提出了一个很好的观点。例如,传递'&amp;'字符串字段中的字符将获得不完整的值。

答案 2 :(得分:0)

请尝试以下表单:

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

答案 3 :(得分:0)

var params = "idclient=" + document.forms[0][0].value + "&total=" + document.forms[0][1].value;
xmlHttp.send(params);

您应该使用encodeURI()

等功能