Go4Expert

Go4Expert (http://www.go4expert.com/)
-   PHP (http://www.go4expert.com/forums/php/)
-   -   PHP and Ajax POST Request (http://www.go4expert.com/forums/php-ajax-post-request-t7999/)

codeguardian 26Dec2007 15:57

PHP and Ajax POST Request
 
I am trying to send a post request to PHP with Ajax. But, I never get the data with $_POST command. I am wondering what is going on. The code i used is below:

.html
--------------------------------
HTML Code:


<html>
<head>
       
 <title>Untitled</title>

<SCRIPT>

function postForm() {
  var xmlHttp;
  try
    {    // Firefox, Opera 8.0+, Safari
    xmlHttp=new XMLHttpRequest();
    }
    catch (e)
    {    // Internet Explorer   
      try
      {
      xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");     
      } catch (e)
        {     
        try
        {
          xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
        } catch (e)
          {     
            alert("ERROR: CAN NOT POST DATA");
          }
        }
    }
   
    try
    {
      xmlHttp.onreadystatechange=function()
      {
      if(xmlHttp.readyState==4)
        {
        alert(xmlHttp.responseText);
        }
      }
      xmlHttp.open("POST","temp2.php",true);
      postStr = "name="+escape(myForm.name.value);
      alert("SENDING: "+postStr);
      xmlHttp.setRequestHeader("Content-Type","application-x-www-form-urlencoded");
      xmlHttp.send(postStr);
    } catch(e)
      {
      alert("ERROR POSTING DATA");
      }

}

</SCRIPT>

</head>
<body>

<FORM METHOD="post" NAME="myForm" onsubmit="postForm()">
<INPUT NAME="name" TYPE="text"><BR>
<INPUT NAME="pass" TYPE="text"><BR>
<INPUT TYPE="submit" VALUE="login">
</FORM>

</body>
</html>

------------------------------------------------
THen i try to get the data by:

<?

echo "NAME: ".$_POST['name']."<BR>\n";
echo "NAME: ".$_GET['name']."<BR>\n";
echo "NAME: ".$_REQUST['name']."<BR>";


?>

None of the methods ($_POST,$_GET,or REQUEST) get the data. What am i doing wrong???

pradeep 29Dec2007 12:20

Re: PHP and Ajax POST Request
 
Practically there is no need to set the request header, and if you set it should look like this
Code: JavaScript

xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");


codeguardian 4Jan2008 07:22

Re: PHP and Ajax POST Request
 
Thank, it works!!!!! :D it dose not work with the header i used or without header.

pradeep 4Jan2008 10:18

Re: PHP and Ajax POST Request
 
Use Prototype JS, http://www.go4expert.com/showthread.php?t=5541


All times are GMT +5.5. The time now is 19:01.