Sorry for a dumb question, but how do you know it won’t get the value? From the code you presented, it doesn’t look like there’s any way to confirm or deny.
You may want to instead var_dump of the $_POST variable, so that you can determine whether the information you need is there or not and then troubleshoot from there.
Also, keep in mind that you’re sending an AJAX request without specifying a “success” function, so there still wouldn’t be any way to check what your PHP echos/dumps. You’d have to do something like this:
$.post("footer.php", {"locationID": post_id}, function (txt) {
alert(txt);
});
With that, I can see the result in the pop up/alert but the php statement doesn’t print the value? Is there a way to print it out in the php and not the alert?
Thanks again
Hi,
I’m simply trying to get the ID of a link so I can pass it to a query. So for example, if can do this:
<?php $locationID = $_POST['locationID']; echo 'Location Id is: '.$locationID; ?>
to ensure that it’s working.
As I’m trying to load a list of properties on a site and by clicking each one, will load them on the page you’re currently on. So I figure, get the ID of the property onclick and pass it to the query. I therefore assume, if the above code works, I can pass the $locationID value to the query to load the info needed. If that helps?
Yes that does indeed. The code in post #7 demonstrates how to achieve that type of communication, where jQuery is used to post an ajax message to a .php file, and the successful output of that php file is passed back to the function, whereupon you can do something with it.
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.
So when the page loads, header.php, footer.php and test.php all get included at the same time, and are used to create the page.
If you perform the echo from that test.php page, there naturally won’t be any post data for it to use.
It’s only when you click a link and the jQuery post occurs on a second access of test.php that the post data will be available, and the output will be sent back to the function as the txt string.
Would you mind listing down step-by-step what you want to achieve and why?
Perhaps that will help us to understand what you need to achieve, and how to help you achieve it.
Sure, no problem.
I have a wordpress template. This consists of a header.php, page.php and footer.php
page.php
This shows the information from wordpress for standard pages: Home, About, Contact etc
page.php calls header.php and footer.php in
footer.php contains a loop which pulls in a list of properties from a custom post type. Each propoerty has a unique ID.
I’m trying to show the details of any of the properties on the page you’re currently on. The site does not need to load a seperate page for each property. So like having an iframe I guess. Therefore, if you’re on About Us. You will see the list of properties. If I click the first property, it will load the details into test.php. I therefore assumed jquery/ajax would best suit as it refreshes page content without reloading the page