...

View Full Version : Ajax and PHP problem



navienavnav
05-15-2011, 01:32 PM
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 :


<!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
echo $_POST['Username'];
?>

Thanks again!

tangoforce
05-15-2011, 01:51 PM
You have the problem right under your nose:



<p id='forum'>Enter Name: <input type="text" name="uname"/></p>


<?php
//Notice anything odd about the name of the text field in your html?
echo $_POST['Username'];
?>

navienavnav
05-15-2011, 05:57 PM
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?


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.

tangoforce
05-15-2011, 06:16 PM
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.

navienavnav
05-16-2011, 06:12 PM
AHAHAHA Yes we all should sleep nicely :D. 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. :D

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 :D :rolleyes: :p



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum