Like others before me, I too have a problem with Ajax POST method and Send. I have set up a Site Search using the Google Ajax Search API, and it works fine. The search term is stored in the variable (?) [COLOR=”red”]query[COLOR=”red”]searchTerm[COLOR=”red”]query
I want to store the search terms in a database so I can see what people are searching for. So I set up a POST method to send the [COLOR=”Red”]searchTerm[COLOR=”Red”]if (isset($_POST[‘searchTerm’]))[COLOR=”red”]searchTerm
When I couldn’t insert into the database, I added a second (temporary) insert using a dummy value as the [COLOR=”red”]else[COLOR=”red”]if
So the problem is clearly that either the Ajax code doesn’t Send correctly, or the php isn’t receiving it. I am stumped to find what is wrong. Here are the two relevant sections of code:
[QUOTE]
//Ajax section
var request = false;
try {
request = new XMLHttpRequest();
} catch (trymicrosoft) {
try {
request = new ActiveXObject(“Msxml2.XMLHTTP”);
} catch (othermicrosoft) {
try {
request = new ActiveXObject(“Microsoft.XMLHTTP”);
} catch (failed) {
request = false;
alert(“fail”);
}
}
}
if (!request) {
return;
}
var url = “
request.open(“POST”, url, true);
var searchTerm = query;
alert(searchTerm);
request.send(searchTerm);
[QUOTE]
/*PHP section
$link = mysqli_connect(‘localhost’, ‘USERNAME’, ‘Password’, ‘DBNAME’);
mysqli_set_charset($link, ‘utf8’);
if (isset($_POST[‘searchTerm’]))
{
$searchterm = mysqli_real_escape_string($link, $
mysqli_query($link, “INSERT INTO searches (SearchTerm, Date) VALUES(‘$searchterm’,CURDATE())”);
mysqli_close($link);
exit();
} else {
$searchterm = mysqli_real_escape_string($link, ‘dummy’);
mysqli_query($link, “INSERT INTO searches (SearchTerm, Date) VALUES(‘$searchterm’,CURDATE())”);
mysqli_close($link);
exit();
}
To repeat, the php is running and always loads the dummy variable from the [COLOR=”red”]else[COLOR=”Red”]isset($_POST[‘searchTerm’])