Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Jan 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    If statements breaking Ajax

    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

    Code:
    <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>
    Last edited by WA; 01-03-2011 at 09:11 PM.

  • #2
    New Coder
    Join Date
    Jun 2005
    Posts
    52
    Thanks
    0
    Thanked 4 Times in 4 Posts
    Code:
    	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.

  • #3
    New to the CF scene
    Join Date
    Jan 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #4
    New Coder
    Join Date
    Jun 2005
    Posts
    52
    Thanks
    0
    Thanked 4 Times in 4 Posts
    Yup, you're right. It'd just pass "undefined" if it isn't declared.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •