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!
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!