quick ajax help

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
dknight3
Forum Newbie
Posts: 11
Joined: Wed Aug 02, 2006 6:04 pm

quick ajax help

Post by dknight3 »

im having some trouble with my ajax login script, it works fine with "GET" but i want to know why it doesnt work with "POST"
heres the code:

Code: Select all

<html>
<head>

function getHTTPObject() {
  var xmlhttp;
  /*@cc_on
  @if (@_jscript_version >= 5)
    try {
      xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (E) {
        xmlhttp = false;
      }
    }
  @else
  xmlhttp = false;
  @end @*/
  if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
    try {
      xmlhttp = new XMLHttpRequest();
    } catch (e) {
      xmlhttp = false;
    }
  }
  return xmlhttp;
}
var http = getHTTPObject();



function get_hashed_password(){
  	var username = document.getElementById("username").value;
  	http.onreadystatechange = handlehashedpassword();
  	http.open("POST", "get_hashed_password.php", true);
  	http.send('username='+escape(username));
}



function handlehashedpassword(){
  	if (http.readyState == 4){
    	var hashed_password = http.responseText;
		document.getElementById('results').innerHTML = hashed_password;
}
}


</head>
<body>


<div>

<form>
Username:
<br>
<input id="username" type="text" name="username" maxlength="12" size="12" onblur="get_hashed_password();">
<br>
Password:
<br>
<input id="password" type="password" name="password" maxlength="12" size="12"></center></td>
<br>
Remember me<input type="checkbox" checked name="remember">
<br>
<input type="submit" value="Login" onclick="login();">
</form>

<form action="register.html" method="LINK">
<input type="submit" value="Register">
</form>

</div>

<div id="results">results here</div>

</body>
</html>
the php file just echos the POST['username']

this is just a test and isnt actually meant to login its just supposed to say the username below the form when you type it in..


EDIT: oh yea the functions are supposed to be in javascript tags, you can test what happens here, http://freescripts.exofire.net/ajax/login.html
User avatar
Buddha443556
Forum Regular
Posts: 873
Joined: Fri Mar 19, 2004 1:51 pm

Post by Buddha443556 »

I'm guessing you need a Content-Type header.

Code: Select all

function get_hashed_password(){
        var username = document.getElementById("username").value;
        http.onreadystatechange = handlehashedpassword();
        http.open("POST", "get_hashed_password.php", true);
        http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        http.send('username='+escape(username));
}
dknight3
Forum Newbie
Posts: 11
Joined: Wed Aug 02, 2006 6:04 pm

Post by dknight3 »

tried it already
Post Reply