trq Posted February 7, 2012 Share Posted February 7, 2012 You need to make a separate page that your Ajax requests. This page should then only return the data that you want. A better option though when working with Ajax is to return Json (which can be easily handled by Javascript as an object). Quote Link to comment https://forums.phpfreaks.com/topic/256243-get-relid-and-pass-to-query/page/2/#findComment-1315401 Share on other sites More sharing options...
jarvis Posted February 7, 2012 Author Share Posted February 7, 2012 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? Quote Link to comment https://forums.phpfreaks.com/topic/256243-get-relid-and-pass-to-query/page/2/#findComment-1315452 Share on other sites More sharing options...
trq Posted February 7, 2012 Share Posted February 7, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/256243-get-relid-and-pass-to-query/page/2/#findComment-1315573 Share on other sites More sharing options...
jarvis Posted February 8, 2012 Author Share Posted February 8, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/256243-get-relid-and-pass-to-query/page/2/#findComment-1315689 Share on other sites More sharing options...
trq Posted February 8, 2012 Share Posted February 8, 2012 But if I echo the variable echo $the_location_id; nothing is shown Where exactly are you trying to echo it? Quote Link to comment https://forums.phpfreaks.com/topic/256243-get-relid-and-pass-to-query/page/2/#findComment-1315719 Share on other sites More sharing options...
jarvis Posted February 8, 2012 Author Share Posted February 8, 2012 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!? Quote Link to comment https://forums.phpfreaks.com/topic/256243-get-relid-and-pass-to-query/page/2/#findComment-1315753 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.