dongshi1914 2011-01-25 20:58
浏览 48

AJAX post方法:变量不会传递给目标php

I am trying to send two pieces of info to a php. 1-) tent = zuzu 2-) zart = gagi

target php simply echoes what I send so that I can check if it's working. This is the javascript:

    function boka ()
{
var mesparam = "tent=zuzu&zart=gagi";
if (window.XMLHttpRequest){xmlhttp=new XMLHttpRequest();}
else {xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");}
xmlhttp.onreadystatechange=function() { if (xmlhttp.readyState==4 && xmlhttp.status==200) {document.getElementById("response").innerHTML=xmlhttp.responseText;} }
xmlhttp.open("POST","/mysite/oxifa/oxifat.php?tent=zuzu&zart=gagi",true); 
//xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
//xmlhttp.setRequestHeader("Content-length", mesparam.length);
//xmlHttp.setRequestHeader("Connection", "close");
xmlhttp.send(mesparam);
}

This is oxifat.php that recieves the request:

    <?php
echo " sign1 <br>";


echo next($_POST);
echo next($_POST);
echo next($_POST);
echo next($_POST);
echo next($_POST);
echo $_POST['tent'];
echo $_POST['zart'];
echo $_REQUEST['tent'];
echo $_REQUEST['zart'];

echo "<br> sign2";

?>

As you can see I've included all sorts of things to echo out whatever is in $_POST but apparently there is nothing there and this is the response I get:

sign1

Notice: Undefined index: tent in C:\wamp\www\mysite\oxifa/oxifat.php on line 16

Notice: Undefined index: zart in C:\wamp\www\mysite\oxifa/oxifat.php on line 17

Notice: Undefined index: tent in C:\wamp\www\mysite\oxifa/oxifat.php on line 18

Notice: Undefined index: zart in C:\wamp\www\mysite\oxifa/oxifat.php on line 19

sign2

three lines about the "setRequestHeader" are in comment status. If I include them, I don't even get sign1. No response. What I figure out from this is everything is OK but I don't seem to understand how to use the post method to pass data to php. How do I do this? I have read everything on the net. I only do not know what "setRequestHeader" is for. One more thing: If I put ?tent=zuzu&zart=gagi at the end of target URL, $_REQUEST thing works. But that's GET and not what I'm trying to do. What is the $_POST's deal?

  • 写回答

3条回答 默认 最新

  • doubaisui2526 2011-01-25 21:06
    关注

    Could you please try to invoke following code

    function getXMLObject() {
      var xmlHttp = false;
      try {
        xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");// For Old Microsoft Browsers
      }
      catch (e) {
        try {
          xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");// For Microsoft IE 6.0+
        }
        catch (e2) {
          xmlHttp = false;// No Browser accepts the XMLHTTP Object then false
        }
      }
      if (!xmlHttp && typeof XMLHttpRequest != 'undefined') {
        xmlHttp = new XMLHttpRequest();//For Mozilla, Opera Browsers
      }
      return xmlHttp;// Mandatory Statement returning the ajax object created
    }
    
    var xmlhttp = new getXMLObject();//xmlhttp holds the ajax object
    
    //use this method for asynchronous communication
    function doRequest(params, callback) {
      if (xmlhttp) {
        xmlhttp.open("POST", "your_script.php?" + params, true);
        xmlhttp.onreadystatechange = function () {
          if (xmlhttp.readyState == 4) {
            if (xmlhttp.status == 200) {
              callback(xmlhttp.responseText);
            }
            else {
              alert("Error retrieving information (status = " + xmlhttp.status + ")
    " + response);
            }
          }
        };
        xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        xmlhttp.send(null);
      }
    }
    

    and in your PHP script first thing off write

    print_r($_POST);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog