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 13 of 13
  1. #1
    New to the CF scene
    Join Date
    Jan 2007
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Passing PHP var from AJAX handler

    Hello all,

    I'm having a problem with the returning a php variable from my ajax php handler.

    I call the handler...

    Code:
    		if(ajaxRequest.readyState == 4 || ajaxRequest.readyState == 0){
    			var ajaxDisplay = document.getElementById('ajaxDiv');
     			var refreshID = "";
    			var timeout = "<?=$timeout?>";
    
    			ajaxDisplay.innerHTML = ajaxRequest.responseText;
    			//Change Refresh Timeout Based on Remaining Song Length
                                           refreshID = setTimeout("DoRefresh()", timeout);
    DoRefresh() function is the ajax function. What am I doing wrong?

  • #2
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,037
    Thanks
    0
    Thanked 250 Times in 246 Posts
    View the HTML source after the page has displayed and check the value of the timeout javascript variable. I don't know PHP but do you really have to put the = sign there?

    Code:
    var timeout = "<?=$timeout?>";

  • #3
    Banned
    Join Date
    Oct 2005
    Location
    I'm in GMT -5
    Posts
    314
    Thanks
    0
    Thanked 1 Time in 1 Post
    unitedbreaks:

    Try structuring it this way:

    Code:
    	var AdminResponse = "";
    
    	function parseSettings(){
    
    		document.getElementById('ajaxDiv').innerHTML = AdminResponse;
                    
    	}
    
    	function obtainSettings(){
    
    		var AdminRequest = window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();   
    		AdminRequest.onreadystatechange = function()
    			{
    		 	 if (AdminRequest.readyState == 4)
    				{
    		 	 	 if (AdminRequest.status == 200)
    					{
    			 	 	 AdminResponse = AdminRequest.responseText;
    			 	 	 parseSettings();
    					}
    		 	 	 else 	{
    				 	 alert('Error Admin.php File '+ AdminRequest.statusText);
    					}
    				}
    			}
    		var forceGET = "?n="+ parseInt(Math.random()*999999999);
    		AdminRequest.open("GET", "Admin.php"+forceGET, true);
    		AdminRequest.send(null); 
    	}

  • #4
    Regular Coder
    Join Date
    Jan 2007
    Posts
    213
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by unitedbreaks View Post
    Hello all,

    I'm having a problem with the returning a php variable from my ajax php handler.

    I call the handler...

    Code:
    		if(ajaxRequest.readyState == 4 || ajaxRequest.readyState == 0){
    			var ajaxDisplay = document.getElementById('ajaxDiv');
     			var refreshID = "";
    			var timeout = "<?=$timeout?>";
    
    			ajaxDisplay.innerHTML = ajaxRequest.responseText;
    			//Change Refresh Timeout Based on Remaining Song Length
                                           refreshID = setTimeout("DoRefresh()", timeout);
    DoRefresh() function is the ajax function. What am I doing wrong?
    Simple error <?=$timeout?> should actually read <?=$timeout;?>

    ALWAYS put the ; at the end of every PHP command.
    Matthew Bagley
    Paramiliar Design Studios
    Website Design | Website Development | Search Engine Optimisation (SEO)

  • #5
    New to the CF scene
    Join Date
    Jan 2007
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by glenngv View Post
    View the HTML source after the page has displayed and check the value of the timeout javascript variable. I don't know PHP but do you really have to put the = sign there?

    Code:
    var timeout = "<?=$timeout?>";
    Yes, the character is required for the shortcut syntax.

    Quote Originally Posted by Ancora View Post
    unitedbreaks:

    Try structuring it this way:

    Code:
    	var AdminResponse = "";
    
    	function parseSettings(){
    
    		document.getElementById('ajaxDiv').innerHTML = AdminResponse;
                    
    	}
    
    	function obtainSettings(){
    
    		var AdminRequest = window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();   
    		AdminRequest.onreadystatechange = function()
    			{
    		 	 if (AdminRequest.readyState == 4)
    				{
    		 	 	 if (AdminRequest.status == 200)
    					{
    			 	 	 AdminResponse = AdminRequest.responseText;
    			 	 	 parseSettings();
    					}
    		 	 	 else 	{
    				 	 alert('Error Admin.php File '+ AdminRequest.statusText);
    					}
    				}
    			}
    		var forceGET = "?n="+ parseInt(Math.random()*999999999);
    		AdminRequest.open("GET", "Admin.php"+forceGET, true);
    		AdminRequest.send(null); 
    	}
    I will try this, thank you Ancora.

    Quote Originally Posted by neomaximus2k View Post
    Simple error <?=$timeout?> should actually read <?=$timeout;?>

    ALWAYS put the ; at the end of every PHP command.
    No, the semi-colon is not required for the shortcut syntax.

  • #6
    New to the CF scene
    Join Date
    Jan 2007
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    It's not working Ancora

    Here is my full AJAX call..

    Code:
    function DoRefresh(){
    	var ajaxRequest;  // AJAX Var
    	
    	//Do Browser Support Code
    	
    	try{
    		// Opera 8.0+, Firefox, Safari
    		ajaxRequest = new XMLHttpRequest();
    	} catch (e){
    		// Internet Explorer Browsers
    		try{
    			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
    		} catch (e) {
    			try{
    				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
    			} catch (e){
    				// Error Alert
    				alert("AJAX Browser Error: Cannot Load Radio Information. Please Download The Latest Mozilla Firefox or Internet Explorer Browser.");
    				return false;
    			}
    		}
    	}
    	
    	// Create a function that will receive data sent from the server
    	ajaxRequest.onreadystatechange = function(){
    	
    		if(ajaxRequest.readyState == 1){
    			
    			var imgeffect = document.getElementById('imgeffect');
    			
    			if (document.getElementById('imgeffect').style.visibility == "hidden") 
    			{
    			var loading = document.getElementById('loading');
    			var loadingdone = document.getElementById('loadingdone'); 
    			
    			document.getElementById('imgeffect').style.visibility = "visible";
    			loading.style.display = "block";
    			loadingdone.style.display = "none";
    			}
    			
    		}
    		if(ajaxRequest.readyState == 2){
    		
    			var imgeffect = document.getElementById('imgeffect');	
    			
    			if (imgeffect.style.visibility == "visible") 
    			{ 
    			var loading = document.getElementById('loading');
    			var loadingdone = document.getElementById('loadingdone');	
    			
    			imgeffect.style.visibility = "hidden";
    		    loading.style.display = "none";
    			loadingdone.style.display = "block";
    			} 
    
    		}
    		if(ajaxRequest.readyState == 4){
    			
    			if (ajaxRequest.status == 200)
    			{
    			var loading = document.getElementById('loading');
    			var loadingdone = document.getElementById('loadingdone');
    			var tunein = document.getElementById('tunein');
    			var refreshID = "";
    			//var timeout = "<?=$timeout?>";
    			var timeout = "30000";
    			var ajaxResponse = "";
    			
    			ajaxResponse = ajaxRequest.responseText;
    			//Change Refresh Timeout Based on Remaining Song Length
    			loading.style.display = "none";
    			loadingdone.style.display = "block";
    			tunein.style.display = "block";	
    			refreshID = setTimeout("DoRefresh()", timeout);	
    			
    			//Finally, Do Refresh
    			parseRefresh(ajaxResponse);	
    			} else {
    			ajaxResponse = "Radio Information Error Code: "+ ajaxRequest.status +", Please contact admin@unitedbreaks.fm";
    			
    			//Do Refresh
    			parseRefresh();
    			}
    	  }
    }
    
    	var forceGET = "?n="+ parseInt(Math.random()*999999999);
    	ajaxRequest.open("GET", "main_include.php"+forceGET, true);
    	ajaxRequest.send(null); 
    }
    
    function parseRefresh(ajaxResponse){
    var ajaxDisplay = document.getElementById('ajaxDiv');
    
    ajaxDisplay.innerHTML = ajaxResponse;
    }
    Then in the handler (main_include.php) , I am simply defining the $timeout variable..

    Code:
      //Set refesh interval
     if($timeleft>0)
       { $timeout = $timeleft; }	# if timeleft is valid, refresh on timeleft
     else
       { $timeout = 60; }	# otherwise, fall back on 60 second refresh
    $timeleft is a variable defined by MySQL data (remaining track length), This dynamic variable works like this..

    If there is 2:00 minutes (120000) milliseconds left on the track.. and someone hits the site (or calls ajax function) , the $timeleft var has exact refresh time.. versus refreshing when not needed (if track hasn't changed)

    http://www.unitedbreaks.fm/index_test.php
    Last edited by unitedbreaks; 02-02-2007 at 12:45 AM.

  • #7
    Banned
    Join Date
    Oct 2005
    Location
    I'm in GMT -5
    Posts
    314
    Thanks
    0
    Thanked 1 Time in 1 Post
    unitedbreaks:

    I wrote: "Try structuring it this way".

    That's not the code I posted. First, the readystates change too fast to use them to affect the display.

    Second, my AdminResponse variable is GLOBAL. I declared it OUTSIDE of any function. You made it a local variable, are are attempting to pass to your parsing function.

    If you are not going test the code I posted AS IS, then I'm finished.

  • #8
    New to the CF scene
    Join Date
    Jan 2007
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Sorry for the confusion Ancora. I tried your code structure before I modified it and it works, but doesn't pass the php var $timeout.

    Code:
    var AdminResponse = "";
    var timeout = ""; // added
    
    	function parseSettings(){
    
    		refreshID = setTimeout("obtainSettings()", timeout); // added	
    		document.getElementById('ajaxDiv').innerHTML = AdminResponse;
                    
    	}
    
    	function obtainSettings(){
    
    		var AdminRequest = window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();   
    		AdminRequest.onreadystatechange = function()
    			{
    		 	 if (AdminRequest.readyState == 4)
    				{
    		 	 	 if (AdminRequest.status == 200)
    					{
    					 timeout = "<?=$timeout?>"; // added
    			 	 	 AdminResponse = AdminRequest.responseText;
    			 	 	 parseSettings();
    					}
    		 	 	 else 	{
    				 	 alert('Error Admin.php File '+ AdminRequest.statusText);
    					}
    				}
    			}
    		var forceGET = "?n="+ parseInt(Math.random()*999999999);
    		AdminRequest.open("GET", "main_include.php"+forceGET, true);
    		AdminRequest.send(null); 
    	}
    its uploaded.. http://www.unitedbreaks.fm/index_test.php

  • #9
    Banned
    Join Date
    Oct 2005
    Location
    I'm in GMT -5
    Posts
    314
    Thanks
    0
    Thanked 1 Time in 1 Post
    unitedbreaks:

    I see. I'd put an alert in the parsing function.

    Code:
    function parseSettings(){
    
                    alert(timeout);
    		refreshID = setTimeout("obtainSettings()", timeout); // added	
    		document.getElementById('ajaxDiv').innerHTML = AdminResponse;
                    
    	}
    Find out exactly what the value is of that variable. The JS looks okay. If timeout, has a value, but is a string, instead of an integer, then try:

    Code:
    refreshID = setTimeout("obtainSettings()", Number(timeout));
    forcing it to be an integer.

  • #10
    New to the CF scene
    Join Date
    Jan 2007
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    value is empty (via alert window)..

    I made sure the value isn't empty via backend php.

    http://www.unitedbreaks.fm/index_test.php

    Next to the Song Title (blue text under loading).. You should see a number counting down.

    Thats $timeout (remaining song length in seconds).
    Last edited by unitedbreaks; 02-02-2007 at 05:31 PM.

  • #11
    Banned
    Join Date
    Oct 2005
    Location
    I'm in GMT -5
    Posts
    314
    Thanks
    0
    Thanked 1 Time in 1 Post
    unitedbreaks:

    I thought it might be. Why don't you echo it along with the other info in the response, then let the JS split them?

    Code:
    <?php
       
        $timeout = 5000;  // this is 5 seconds
        $otherInfo = "This will be displayed in the div";
    
        $echo $timeout . "|" . $otherInfo;
    
    ?>
    Code:
    function parseSettings(){
    
                    var nInfo = AdminResponse.split("|");
    		refreshID = setTimeout("obtainSettings()", Number(nInfo[0]));
    		document.getElementById('ajaxDiv').innerHTML = nInfo[1];
                    
    	}

  • #12
    New to the CF scene
    Join Date
    Jan 2007
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you very much for your help Ancora! Works perfectly

  • #13
    Banned
    Join Date
    Oct 2005
    Location
    I'm in GMT -5
    Posts
    314
    Thanks
    0
    Thanked 1 Time in 1 Post
    unitedbreaks:

    You're welcome. Good luck with the remainder of your project.


  •  

    Posting Permissions

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