xmlHTTPRequest POST不发送数据

时间:2013-06-13 04:18:03

标签: php mysql post xmlhttprequest

我有xmlHTTPrequest GET脚本工作正常,但由于服务器问题,我不得不将其更改为POST方法。我无法获取$ _POST变量中的数据。当我签入CHROME INSPECTOR调试工具时,GET Method状态为200 ok。需要帮助来查看javascript是否正确。

xmlHTTPrequest文件:

    <script type="text/javascript">

        function showprodes(str2)
        {
        var q2 = encodeURIComponent(str2);
        if (window.XMLHttpRequest)
          {// code for IE7+, Firefox, Chrome, Opera, Safari
          xmlhttp=new XMLHttpRequest();
          }
        else
          {// code for IE6, IE5
          xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
          }
          var url = "http://www.amg.in/amogtst/rateprod.php";
        xmlhttp.open("POST", url, true);
        xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");  
        xmlhttp.send(q2);
         }


    </script>

    <?

        $result2 = mysql_query("SELECT Prod_desc FROM PRODMAST ORDER BY Prod_desc");

        echo "<form name='f1'>";

        echo " <span class='style3'>Gas Type &nbsp;</span> <select name='Proddesc' onchange=\"showprodes(this.value);\"><option value=0>Select a Product</option>";
        while($nt2=mysql_fetch_assoc($result2))
        {
        echo "<option value='$nt2[Prod_desc]'>$nt2[Prod_desc]</option>";
        }
        echo "</select>";// Closing of list box

            echo "</form>";
    ?>      

根据第一个php:rateprod.php文件中的用户选择更新表的第二个脚本:

<?php
$q=$_POST['q2'];
$q2=mysql_real_escape_string($q);

include_once 'db.php';

mysql_query("UPDATE RATEMASTER_draft SET Prod_desc='$q2'");

?>  

1 个答案:

答案 0 :(得分:1)

从查看您的AJAX代码,您没有正确提供POST变量。提供给xmlhttp.send()的POST字符串的格式需要与GET字符串的格式相同。尝试使用xmlhttp.send("q2=" + q2)

BTW,为了将来参考,您可以使用print_r($_POST)来显示所有POST变量的内容。这对于调试来说非常方便。

相关问题