www.webdeveloper.com
Results 1 to 5 of 5

Thread: Ajax send()

Hybrid View

  1. #1
    Join Date
    Jul 2013
    Posts
    27

    Ajax send()

    Hi all,

    I have the following html/js code

    Code:
    <html>
    <head><title>Ajax_Testing</title></head>
    <body>
    <form id="ajax_request" method="POST">
    <dl>
    <dt>UserName</dt>
    <dd><input type="text" name="us" id="us"/></dd>
    <dt>Password<dt>
    <dd><input type="text" name="pw" id="pw"/></dd>
    </dl>
    <button type="submit" id="sb">Submit</button>
    </form>
    <script>
    function createXMLHttpRequestObject()
    {
      var Ajax;
    
      try
      {
        Ajax = new XMLHttpRequest();
      }
      catch (exception) 
      {
        try
        {
          Ajax = new ActiveXObject("Microsoft.XMLHttp");
        }
        catch(exception) {}
      }
    
      if(!Ajax)
      {
        alert("Error, your browser does not support Ajax");
      }
      else if (Ajax)
      {
        return Ajax;
      }
    
    }
    
    
    function openHttpRequest(session,rtype,url,async)
    {
      session.open(rtype,url,async);
      session.onreadystatechange = function(){ ReadyStateHandler(session) }; 
      session.send("password=a_secret");
      return session;
    }
    
    function ReadyStateHandler(session)
    {
      if(session.readyState == 4)
      {
    	if(session.status==200)
    	{
    	var Responce = session.responseText;
    	alert(Responce);
    	}
      }
    };
    
    
    function sendRequest(Id)
    {
      var sb_box = document.getElementById(Id);
      sb_box.onclick= function() {openHttpRequest(AjaxSession,"POST","ajax.php",true);return false}
    }
    
    
    
    var AjaxSession = createXMLHttpRequestObject();
    sendRequest("sb")
    
    
    </script>
    </body>
    </html>
    and the following php code which goes along side

    Code:
    <?php
    
    if(isset($_POST['password']))
    {
    $password = $_POST['password'];
    }
    else
    {
    $password = "no password";
    }
    echo $password;
    
    ?>
    I have the following problem:

    When the Ajax is requested the POST value of password does not seem to be given to the php code (atleast correctly) though when looking in the network parameters the password=a_secret is actually sent.

    I think this is most likely something simple ... any ideas?

    Thanks in advance to any replies.

  2. #2
    Join Date
    Dec 2013
    Posts
    63
    Is variable $password inited/used before the line 'if(isset($_POST['password']))'? If not, try to add '$password = "";' before this 'if(...)' line.

  3. #3
    Join Date
    Jul 2013
    Posts
    27
    Hi Blasphemy

    Tried this, no luck.
    Its as if the ajax.php file does not see the password=a_secret as $_POST['password']

  4. #4
    Join Date
    Dec 2013
    Posts
    63
    I think I found it. Try to add this:
    Code:
    session.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    before
    Code:
    session.send("password=a_secret");
    Last edited by blasphemy; 01-21-2014 at 02:30 PM.

  5. #5
    Join Date
    Jul 2013
    Posts
    27
    Yep that worked. When I went over the page in which i was learning Ajax from, i completly missed out this part.

    Thanks for your help m8.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles