View Full Version : Simple AJAX, help needed

gold killer
07-28-2006, 10:47 PM
I'm abit new to ajax, one of my friends helped me with an ajax script for an old site, he is now on holiday so i cant ask him. I wanted to give editing it a shot to try and login, so i had to add another value and edit the parameters, now it doesnt seem to work.

The javascript is:

var http_request = getHTTPObject();
var url = "post_login.php?";

function getHTTPObject() {
var xmlhttp = false;
if (window.XMLHttpRequest){
xmlhttp = new XMLHttpRequest();
else if (window.ActiveXObject){
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
catch (e){
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
catch (e){}

return xmlhttp;

// Ajax
function makePOSTRequest(id) {

it = document.getElementById("username").value +
parameters = 'username=' + encodeURIComponent(it) + '&password=' + encodeURIComponent(it) + '&id=' + encodeURIComponent(id);
http_request.open('POST', url, true);
http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http_request.onreadystatechange = alertContents;

function alertContents() {
if (http_request.readyState == 1) {
document.getElementById('result').innerHTML = '<img src="loading.gif" />';
if (http_request.readyState == 4) {
results = http_request.responseText;
document.getElementById('result').innerHTML = results;

The html im using is:

Username:<br /> <input type=\"text\" name=\"username\" class=\"nav\" /><br />
Password:<br />
<input type=\"password\" name=\"password\" class=\"nav\" /><br />
<input type=\"button\" class=\"nav\" onclick=\"javascript: makePOSTRequest('".$profile[id]."')\" value=\"Login!\"><br />
<strong><a href=\"/index.php?y=register\">Not Registered?</a></strong>");

And the php page the ajax is using (post_login.php) is:

include ("config.php");
$id = intval($_POST[id]);

$username = $_POST['username'];
$password = md5($_POST['password']);

$info = mysql_query("SELECT * FROM users WHERE username = '$username'") or die(mysql_error());
$data = mysql_fetch_array($info);
if($data[password] != $password)
echo "Incorrect username or password!";
setcookie("essex_id", $logged[id],time()+(62*60*23*10), "/", "");
setcookie("essex_pass", $logged[password],time()+(62*60*23*10), "/", "");
$_SESSION[essex_id] = $logged[id];
$_SESSION[essex_pass] = $logged[password];

Please can someone help me,


07-29-2006, 05:04 AM
Download Firefox and install Firebug will make your life easier.

What is the error you are getting? Is it making it to the server?

One thing I see:
You need to check for a status of 200 when you get a readystate of 4.


gold killer
07-29-2006, 12:32 PM
how do i do that?

When i click login nothing happens, no error just no attempt to submit the data

07-30-2006, 01:03 AM
This is just an answer to my problems - if you get a functioning results please let me know. I wonder why I didnt see this thread before. Im a begginer in Ajax just like you are but I guess you're a bit further than I am but I intend to spend the next available 10 days practising.

Talking about rumbling. :confused:

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum