Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    May 2011
    Location
    India
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Ajax and PHP problem

    Hello. I've just begun learning php and ajax yesterday. For learning's sake, I made a page which has an username and password entry and when the submit button is hit, it's supposed to supply that data to a php file using ajax and handle the response from the php file which is nothing but a the username itself and display it in another empty text field (as i mentioned, i am just experimenting, there is no actual purpose).

    But nothing happens when I push the submit button. Please have a look at the code and guide me where I am wrong. I'd be grateful to you!

    This is my code for html page :

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html>
    <head>
      <title>Register!</title>
      <link rel="stylesheet" type="text/css" href="style.css"/>
    </head>
    <body>
    <div id="wrap">
    <script type="text/javascript">
     function hit()
      {
        var ajaxRequest;
    	 try 
    	   { 
    	    ajaxRequest= new XMLHttpRequest();
    		}
    	 catch (e){
    		try{
    			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
    		} catch (e) {
    			try{
    				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
    			} catch (e){
    				alert("Your browser sucks!");
    				return false;
    			}
    		}
    		
    	
        ajaxRequest.onreadystatechange=function()
    	 {
    	  if (ajaxRequest.steadystate==4)
    	   {
    	    document.data.res.value = ajaxRequest.responseText;
    	   }
    	  }
    	  var unamevalue=encodeURIComponent(document.getElementById("uname").value)
          var passvalue=encodeURIComponent(document.getElementById("pass").value)
          ajaxRequest.open("POST","process.php",true);
          xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded;"); 
          ajaxRequest.send("Username="+unamevalue+"&Password="+passvalue);
    	  
    }
    </script>  
    <h1>Register!</h1>
    <br/><br/><br/><br/><br/></br></br>
    <form name="data"
    <p id='forum'>Enter Name: <input type="text" name="uname"/></p>
    <p id='forum'>Enter Password: <input type="password" name="pass"/></p>
    <input type="BUTTON" onClick="hit();" value="Submit"/>
    <br/>
    <input type="text" name="res"/>
    </form>
    </div>
    </body>
    </html>

    And this is the code for the php file...

    PHP Code:
    <?php
     
    echo $_POST['Username'];
    ?>
    Thanks again!

  • #2
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,439
    Thanks
    62
    Thanked 537 Times in 524 Posts
    You have the problem right under your nose:

    Code:
    <p id='forum'>Enter Name: <input type="text" name="uname"/></p>
    PHP Code:
    <?php
    //Notice anything odd about the name of the text field in your html?
     
    echo $_POST['Username'];
    ?>
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #3
    New to the CF scene
    Join Date
    May 2011
    Location
    India
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I am sorry but I still don't get it. If you are implying that the variable names 'uname' and 'Username' are different then what about the statements below?

    Code:
    var unamevalue=encodeURIComponent(document.getElementById("uname").value)
          var passvalue=encodeURIComponent(document.getElementById("pass").value)
          ajaxRequest.open("POST","process.php",true);
          xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded;"); 
          ajaxRequest.send("Username="+unamevalue+"&Password="+passvalue);
    And if that's not what you meant then what is it that you meant?
    Like I said, I've just started to learn these things yesterday and I am not posting this question without googling enough so please be patient with me.

  • #4
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,439
    Thanks
    62
    Thanked 537 Times in 524 Posts
    I really did get up far to early today.. my eyes must of been very blurry..

    Have you checked the server logs to see if the request from the ajax is there? - You should find a 200 status code too.
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #5
    New to the CF scene
    Join Date
    May 2011
    Location
    India
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Nevermind

    AHAHAHA Yes we all should sleep nicely . I woke up after a good night's sleep and when i looked at my code, I saw what a mess it was! Really a mess indeed! So many syntax errors, bracket errors and I didn't even assign id's to the form elements and i was using getelementbyid method.

    I was seriously messed up for sure. Well thanks for all your help anyway. It is much appreciated.

    This topic should be deleted because it's nuking nature


  •  

    Posting Permissions

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