0

this may be related to passing php variables to javascript,

but say i have an ajax function ( call this file1.php)

that calls a php file ( call this file2.php) that itself has ajax in it.

which, say calls for a file3.php

in file1.php, i collect information that i put into a querystring, passed to file2.php.

file2.php can use the querystring info via $_GET

but how do i tell file2.php to incorporate the variable into the querystring of its ajax calls to file3.php?

currently i have this- it looks right for the most part but won't work


file1.php

<html>
<body>


<script language="javascript" type="text/javascript">

function gotofile2(){
	var ajaxRequest; 
	
	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){
				// Something went wrong
				alert("Your browser broke!");
				return false;
			}
		}
	}
	
	ajaxRequest.onreadystatechange = function(){
		if(ajaxRequest.readyState == 4){
			var ajaxDisplay = document.getElementById('ajaxDiv');
			ajaxDisplay.innerHTML = ajaxRequest.responseText;
		}
	}
	var key = document.getElementById('key').value;
	
	var queryString = "?key=" + key;
	ajaxRequest.open("GET", "file2.php" + queryString, true);
	ajaxRequest.send(null); 
}

</script>



<form name='myForm'>
key: <input type='text' id='key' /> <br />

<input type='button' onclick='gotofile2()' value='something' />

</form>
<div id='ajaxDiv'>result</div>
</body>
</html>

file2.php

<html>
<body>
<?
$key = $_GET['key'];
echo $key;
?>

<script language="javascript" type="text/javascript">

function gotofile3(){
	var ajaxRequest; 
	
	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){
				// Something went wrong
				alert("Your browser broke!");
				return false;
			}
		}
	}
	
	ajaxRequest.onreadystatechange = function(){
		if(ajaxRequest.readyState == 4){
			var ajaxDisplay = document.getElementById('ajaxDiv');
			ajaxDisplay.innerHTML = ajaxRequest.responseText;
		}
	}
	var key = <? echo $key?>;
	// this var key bit is the bit i can't get


	var queryString = "?key=" + key;
	ajaxRequest.open("GET", "file3.php" + queryString, true);
	ajaxRequest.send(null); 
}

</script>



<form name='myForm'>


<input type='button' onclick='gotofile3()' value='something' />

</form>
<div id='ajaxDiv'>result</div>
</body>
</html>
1
Contributor
1
Reply
2
Views
7 Years
Discussion Span
Last Post by aqhulcus
0

perhaps an example of how i'd use it might help explain?

say i have file 1 as a login type thing where you can type in your id

i then pass on the id to file 2, where it displays certain information depending on your id ( e.g if the id says you don't have access to item a, it won't be displayed)
then the items you can see have buttons that allow you to perform a certain action

which is an ajax call to file 3, where i need the user id to modify their database info.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.