PHP/AJAX Response issue – PHP

  php

Q(Question):

Ok. So I have a PHP function that is supposed to be sending back a customized string that the AJAX will interpret and automatically update/select an option in a select menu. The problem I am running into is that the AJAX isn’t receiving anything (or it would seem). Not sure if I am completely overlooking something or if my code just sucks. I will post the particulars below.

//PHP code sending the string out….


$newMake = strtolower($_REQUEST['make']);
$query = "SELECT * FROM make";
$result = mysql_query($query)or die($query." -- ".mysql_error());
$bool = true;
while($row=mysql_fetch_array($result, MYSQL_ASSOC))
{
if(strtolower($row['Name']) == $newMake)
{
$bool = fasle;
}
}
if($bool)
{
$newMake = ucfirst($newMake);
$query = "INSERT INTO make (Name) VALUES ('$newMake')";
$result = mysql_query($query)or die($query." -- ".mysql_error());
if($result)
{
$query = "SELECT * FROM make WHERE Name = '$newMake'";
$temp = mysql_query($query)or die($query." -- ".mysql_error());
$row=mysql_fetch_array($temp, MYSQL_ASSOC);
}
echo "True,".$newMake.",".$row['makeID'].",MAKE"; //<-- THIS PART
}
else
echo "False";
//Javascript handling the return....
function insert(url)
{
var request;
request = getHTTPObject();
request.onreadystatechange=function()
{
if(request.readyState == 4)
{
if(request.responseText == "")
{
alert("Son of a....");
}
else
{
response = request.responseText;
alert(response);
response = response.split(",");
if(response[0] == "True" && response[3] == "MAKE")
{
var select = document.getElementById('makeList');
var option = document.createElement('option');
option.text = response[1];
option.value = response[2];
option.selected = true;
select.add(option, null);
sendRequest(document.getElementById('modelList'), 'loadMake.php?id='+option.value);
alert("Added "+response[1]+" to database.");
}
}
}
else if(request.readyState == 1)
{
//waiting...
}
}
request.open("POST",url,true);
request.send(null);
}

Now, the database updates each time, but the alerts NEVER fire because the response is always blank. Any ideas?

A(Answer):

do you have a demo page?

usually, you check for readyState 4 and status 200 before proceeding.

A(Answer):

Alright, got a test site up here: http://boutwell.isa-geek.com/Temp/. As you should see, when you decide to add a make, instead of adding it, then responding with the string "TRUE, MAKENAME, MAKEID, MAKE", I get no response and thus the AJAX has no idea what to do. Also, I initially had the function check the status of the request, but it seems that no matter what I do, I get a 0 as the status. When I took that check out, it worked perfectly for a while… now this.

A(Answer):

not sure what went wrong on your side, but it worked for me …

A(Answer):

It added then automatically selected the element you added?

A(Answer):

Nevermind, I figured it out… not sure why this matters, but all I had to do was change the <a href="" otherstuff/> to <a href="#" otherstuff/> – any ideas why that was such a big deal?

A(Answer):

yepp, it did.

LEAVE A COMMENT