Jump to content

Recommended Posts

I seriously cant believe this is so difficult. I simply want to grab a value, assign it to php and then use it in a query.

 

Is there a simple option I've overlooked? Surely if

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

displays the value, there must be a simple way to not alert it to the public and assign it to $this_value or something, which can then be used anywhere on the page?

You are getting the value into php. Your problem is that your not returning the data you want from php. Well you are, but your aslo returning a bunch of htm along with it.

 

Ajax request are no different to a normal request. whatever your php script outputs will be returned.

Ok. I've been playing around but am still none-the-wiser! So if I post my code, it may help as what you're saying implies that yes, I can output the php value as I have captured it.

I've got header.php with this:

<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);

	$.post("test.php", {"locationID": post_id}, function (txt) {
		alert(txt);
	});


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

I then have footer.php which lists all the dynamic links:

		<ul>
		<?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();
		?>												
		</ul>

In footer.php, I also include a call to a file:

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

In test.php I want to show the result of the selected link:

echo 'Footer with location id of ' . $_POST['locationID'];
$the_location_id = $_POST['locationID'];

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

 

So As you can see, I'm trying to get the id of the clicked link, pass it to a php variable which I can then use in a query. From what's shown above, surely that's correct? But if I echo the variable

echo $the_location_id;

nothing is shown

 

Sorry to keep posting but I really would like to get to grips with it and see what I've done wrong.

 

Thanks again

I'm trying to output it on test.php

 

However, I'm starting to think that as the file test.php is being loaded independtly (i.e. called in). It will error as the file will need to be calling in some of the core functions of Wordpress - but I may be wrong!?

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.