PHP and Ajax POST Request

Discussion in 'PHP' started by codeguardian, Dec 26, 2007.

  1. codeguardian

    codeguardian New Member

    Joined:
    Oct 26, 2007
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
    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:
    
    <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???
     
    Last edited by a moderator: Dec 26, 2007
  2. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,645
    Likes Received:
    87
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    http://blog.pradeep.net.in
    Practically there is no need to set the request header, and if you set it should look like this
    Code:
    xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
    
     
  3. codeguardian

    codeguardian New Member

    Joined:
    Oct 26, 2007
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
    Thank, it works!!!!! :D it dose not work with the header i used or without header.
     
  4. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,645
    Likes Received:
    87
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    http://blog.pradeep.net.in

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice