Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

php and jquery with variables 2

Status
Not open for further replies.

dinger2121

Programmer
Sep 11, 2007
439
US
Hello,
I am creating some sites using php with jquery. On each page, I have a php variable that determines what the site id is.
I then have a jquery script defined which will load a section of the page at the load (contained within a div tag) and then refresh that same piece every few seconds. This portion of the page is loaded by calling a second included php script. I would like this second script which is called by the jquery function to return specific data based upon the site id variable defined in the page.

This site id variable is not making it into the second script that is called by the jquery. I define the variable on the first line of the page. Does anyone know how I can get this variable to go through?
I had considered making a hidden field which contains the site id that jquery can then retrieve, but I still don't know how to pass it into the second script which renders the site specific data.

hope that makes sense.

Thanks for any help.

carl
MCSD, MCTS:MOSS
 
Can you post some sample code? I think your plan makes sense, but its hard to tell exactly where the problem is from your description.

-----------------------------------------
I cannot be bought. Find leasing information at
 
By using jquery your second PHP script is effectively running alone, independent and unaware of the first PHP script. Which means any variables defined there will always be unavailable to it.

If you need to get content based on an ID, I would deliver the content from the first PHP script, and then just use jquery to manipulate it as necessary.

You are basically attempting to mix to environments which run independently in 2 different locations.

PHP will run on the server which means by the time jquery is going to do its thing PHP is already done.

Using an ajax interaction you can explicitly send the variable to your second PHP script. By passing it as a url parameter
Code:
 httpObject.open("GET", "secondscript.php?[red]myvariable[/red]=" + IDvalue );

The your second PHP script can access the value from the GET super global.
Don't know much about jquery, so you'll need to figure out how to incorporate the ajax call into it.





----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Thanks Phil,
I tried using the query string when calling the second script to pass in the site id, but when I do that, the second script won't run - nothing is returned. I even tried putting just a simple <?php echo "test" ?> for my second script, but with the query string parameter in there, nothing runs at all.

carl
MCSD, MCTS:MOSS
 
the jquery stuff is easy.

to answer the php question properly please provide the code you are using. However the easiest way of grabbing the siteID in jQuery is to output it using php during page load

Code:
<head>
<?php 
$siteID = 'somevar';
echo <<<JS
<script type="text/javascript">
var siteID='$siteID';
var scriptURL = '[URL unfurl="true"]http://mydomain.com/path/to/script.php';[/URL]
</script>
JS;
?>
... more head stuff
</head>

then in jQuery
Code:
$('#myResultsDiv').load(scriptURL, {siteID: siteID});
 
I was able to get it to do what I want using the following code -
Code:
<script>
	 $(document).ready(function() {
		 $("#content_1").load("include/script.include.php?q=1");
	   var refreshId = setInterval(function() {
		  $("#content_1").load('include/script.include.php?q=1');
	   }, 5000);
	});
	</script>

this works fine, except that the siteId (1) is hard coded.

carl
MCSD, MCTS:MOSS
 
If that works then it should be easy to set the ID dynamically once you have it.

Use jpadie's example, and stick the site Id into a JS variable, and the just plug that Js variable into your call.

Code:
echo <<<JS
<script type="text/javascript">
var siteID='$siteID';
</script>
JS;
?>
...
$("#content_1").load("include/script.include.php?q=" [red]+ siteID[/red]);

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Thanks - I will give that a try

carl
MCSD, MCTS:MOSS
 
Or just pass an object as the second argument of jquery.load
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top