Pass jquery value to php variable

Using .load() it’s a GET request that goes to the server, not a POST request.

Thanks again but:


$the_location_id = $_GET['locationID'];
echo '<br>Loc ID '. $the_location_id;

Still not showing anything or have I been really thick?

Possibly. Do you have a test page?

Not online as developing locally. To clarify, I now have:
Header.php


<script>
$(document).ready(function(){
	$.ajaxSetup({cache:false});
	$(".locationID").click(function(){
		var post_id = $(this).attr("rel");
		//alert($(this).attr('rel'));
		$("#single-home-container").html("loading...");
		$("#single-home-container").html(post_id);
		
		$("#single-home-container").load("test.php", { 'locationID': $(this).attr("rel") } );
		
		//$.post("test.php", {"locationID": post_id}, function (txt) {
		//	alert(txt);
		//});

		return false;
	});
});
</script>

Test.php


<div id="single-home-container"></div>

<?php
$the_location_id1 = $_GET['locationID'];
echo '<br>Loc ID '. $the_location_id1;
?>

It still has the remaining loop

footer.php
the same as post #14
Thanks

It’s not allowed for a page to have multiple unique identifiers, as it tends to go against the idea of them being unique, so you’ll need to remove that div from the test.php code.

After that, check that the click event is being successfully run.

Thanks but removing

<div id="single-home-container"></div>

from test.php doesn’t seem to make a difference.
Header.php


<script>
$(document).ready(function(){
	$.ajaxSetup({cache:false});
	$(".locationID").click(function(){
		var post_id = $(this).attr("rel");
		//alert($(this).attr('rel'));
		$("#single-home-container").html("loading...");
		$("#single-home-container").html(post_id);
		
		$("#single-home-container").load("test.php", { 'locationID': $(this).attr("rel") } );
		
		//$.post("test.php", {"locationID": post_id}, function (txt) {
		//	alert(txt);
		//});

		return false;
	});
});
</script>	
	

footer.php has the loop tp produce the links:


			<?php	
			$args = array(
						  'post_type' => 'locations',
						  'order'    => 'DESC'
						  );
			$loop = new WP_Query( $args );
			while ( $loop->have_posts() ) : $loop->the_post();
			?>	
				<li><a href="#" rel="<?php the_ID(); ?>" class="locationID"><?php the_title(); ?></a></li>
			<?php	
			endwhile;	
			wp_reset_query();
			?>	

Also is the include to test.php


<?php include(TEMPLATEPATH."/test.php"); ?>

Test.php has this to check the value


<?php
$the_location_id1 = $_GET['locationID'];
echo '<br>Loc ID '. $the_location_id1;
?>

And the main query I wish to pass the value to:


			<?php	
			$args = array(
						  'post_type' => 'locations',
						  'posts_per_page' => 1,
						  'p' => $the_location_id1 ,
						  );
			$loop = new WP_Query( $args );
			while ( $loop->have_posts() ) : $loop->the_post();
			?>	
			<?php the_title(); ?>
			<?php	
			endwhile;	
			wp_reset_query();
			?>

Which i believe is the right set up from what’s been posted so far!?
Thanks again, it really is appreciated

Check to find out how far things get before they start working.

[list=1][]Does the click event get triggered?
[
]Is the value in the REL attribute successfully retrieved?
[]Is a request sent for the test.php file?
[
]Does the $_GET variable contain the expected information?
[*]Is test.php outputting something that can be used?[/list]

That is the order in which you need to to check things out.

1.Does the click event get triggered? - Yes
2.Is the value in the REL attribute successfully retrieved? - Yes, I’ve got

alert($(this).attr('rel'));

which shows the value
3.Is a request sent for the test.php file? Yes as I’ve got

<div id="single-home-container"></div>

this shows the value
4.Does the $_GET variable contain the expected information? No as this code isn’t outpuuting a value

$the_location_id1 = $_GET['locationID'];
echo '<br>Loc ID '. $the_location_id1;

5.Is test.php outputting something that can be used? See point 4

It’s so close, it must be!?

Thanks again.

What do you mean it shows the value?
Before or after the click?
The way to check that the request is being sent is to investigate your apache log to find out when the request for that file occurs.

Or you can use the Network section of a web development tool to look for the file request when you make the click.

With Google Chrome you would use Ctrl+Shift+J and open the Network section before clicking.
With Firefox+Firebug you would open up Firebug and enable the network section from your page before clicking.
With Internet Explorer you would use F12 to open up the developer section and go to Networking, and start capturing before clicking.

Is a request sent for the test.php file?

What you need to see is something like this:

Request URL:http://www.somedomain.com/test.php?locationID=3
Request Method:GET
Status Code:200 OK

Now when the page loads either, but when you click on the link.

What do you mean it shows the value?

Sorry! On loading the site, nothing is shown. If I click a link 1) I get a pop up with a value e.g. 9. Then on the page where the

<div id="single-home-container"></div>

would be I see 9

If I use Chrome and do ctrl=shift=j I see this:

http://localhost/domain/test.php Failed to load resource: the server responded with a status of 404 (Not Found)

:confused:
So guess it’s not working as I though

If I change the path in the header script to a path of footer.php I get this error:

Fatal error: Call to undefined function query_posts()

If I remove

<div id="single-home-container"></div>

the error goes but I dont see anything like

Request URL:http://www.somedomain.com/test.php?locationID=3
Request Method:GET
Status Code:200 OK

That seems to indicate that the test file cannot be accessed.

That seems to confirm that it can at least access a file from your localhost

If the container doesn’t exist, jQuery won’t be able to run the .load method

So you need to figure out why your local testing environment can’t access that test.php file.