...

View Full Version : If statements breaking Ajax



mrtechguy
01-03-2011, 01:00 PM
Hi All,
I have the below code in a website and I have tried to add in a "toolbox" with things to do to the transations, and I figured that instead of having 3 different ajax functions to handle the 3 different pages that could be called, I should be able to use if statements to determine which page to call and what to send in the query. All of the code works fine if I remove the if statements (and the 2nd section for the "toolbox" however if I put them back in the request that is sent to the server never had the value of "transid". The server receives this request and sends back a response, however I never get this response, or it is never rendered to the page (I have even tried forcing the PHP file to send back an output.

I can't for the life of me work out what is wrong with it, so is anyone able to make some suggestions as to why the following isn't working. The code is contained within the <head> tags.

Thanks
MTG



<script>
function callback(serverData, serverStatus) {
document.getElementById("transactionbox").innerHTML=serverData;
}
function submitForm(whereTo) {
var ajax;
try { ajax = new ActiveXObject('Msxml2.XMLHTTP'); }
catch (e)
{
try { ajax = new ActiveXObject('Microsoft.XMLHTTP'); }
catch (e2)
{
try { ajax = new XMLHttpRequest(); }
catch (e3) { ajax = false; }
}
}
ajax.onreadystatechange = function()
{
if(ajax.readyState == 4 || ajax.readyState=="complete")
{
callback(ajax.responseText, ajax.status);
if(whereTo=="additem") { // = becomes ==
document.getElementById('barcode').value="";
document.getElementById('barcode').focus();
document.getElementById('toolbox').style.display = "block";
}
}
}
var ajaxdata;
if(whereTo=="additem") {
ajaxdata = "barcode="+document.getElementById('barcode').value+"&transid="+document.getElementById('transid').value;
ajax.open("POST", "additem.php", true);
}
else if(whereTo=="toolbox") {
if(toolBox=="discount") {
ajaxdata = "mode=discount&transid="+transID;
ajax.open("POST", "toolbox.php", true);
}
}
ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
ajax.send(ajaxdata);
}
</script>

Zefris
01-04-2011, 04:49 AM
else if(whereTo=="toolbox") {
if(toolBox=="discount") {
ajaxdata = "mode=discount&transid="+transID;
ajax.open("POST", "toolbox.php", true);
}
}


transID doesn't seem to be defined anywhere, at least on that block of code, so that may be the reason why the backend isn't getting a transid value.

mrtechguy
01-04-2011, 06:51 AM
This isn't the problem (I forgot to change it after trying to pass the transid using the function vars), however now I am using document.getElementByID('transid').value there as well. Surely if the var wasn't defined it wouldn't stop ajax passing transid=undefined to the PHP file though?

Thanks,
MTG

Zefris
01-05-2011, 06:34 AM
Yup, you're right. It'd just pass "undefined" if it isn't declared.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum