0

I'm using WP_Ajax_Response(); to respond to some ajax and output it to a div, everything works great. But how exactly do I respond if I want to output some php in to a div?

A wordpress loop for example. I assume can't just do $response = include 'loopHandle.php'; and then $("body.blog #content").html(this.data);

I've seen some methods that make a call directly to your file and then use wp_load(); but I've heard that's something we should stay away from.

I also know that an AJAX response must be in JSON or XML, so how do I get php into my div?

Example

On this click event:

$link_url = esc_url(wp_nonce_url( admin_url('admin-ajax.php?&action=dynamic_date&my_date='.$date), "dynamic_date_nonce")); ?>
<li><a href='<?php echo $link_url; ?>' id='sort-month'>
<?php _e($fmonth,'sort-month'); ?></a></li>

I send over my action, nonce and date key-pairs. I then capture those key-pairs using something like:

var s = {};
s.response = 'ajax-response';
s.type = "GET";
s.url = sortbydate.ajax_url;
s.data = $.extend(s.data, { action: url.action, _ajax_nonce: url._wpnonce, my_date: url.my_date });
s.global = false;
s.timeout = 30000;
s.success = function(r) {
res = wpAjax.parseAjaxResponse(r,this.response);
$.each( res.responses, function() {
switch(this.what) {
case "fulldate":
$("body.blog #content").html(this.data);

The important variable here is the my_date key-pair. The action then kicks off this function in my functions.php.

function dynamic_date() {
check_ajax_referer('dynamic_date_nonce');
$fdate = $_GET['my_date'];
$response = new WP_Ajax_Response();
$response->add(array(
'what' => 'fulldate',
'data' => $fdate)); //get and send the data you want to front end.
$response->send();
} //end action jackson

Which outputs "2012-9" or whatever date I click on. That's great, works fine for test purposes, but now I want to use that date by exploding it into an an array and use those variables in a wordpress loop.

Currently I have the loop in a file called loopHandle.php, but it's not making use of the date variables as if my understanding is correct I can't access them from loopHandle.php? If I could, then I'd just use $fdate = $_GET['my_date']; in loopHandle.php and finish my loop.

I'm still left with the conundrum of loading loopHandle.php into my div though.

The one method I assume is the way to go about it is to form the loop in my functions.php, somehow convert it into JSON or XML and out put it on my page.

4
  • Please post more code to put your question into context.
    – s_ha_dum
    Sep 16, 2013 at 15:13
  • I see nothing that saves anything to the database. What am I missing? Am I just blind?
    – s_ha_dum
    Sep 16, 2013 at 15:38
  • Sorry, what about the database? :D
    – UzumakiDev
    Sep 16, 2013 at 15:47
  • @s_ha_dum It seems that his guy has done what I'm looking to do stackoverflow.com/questions/11887760/… I've added dataType html to my object and added the loop to my functions.php but still nothing.
    – UzumakiDev
    Sep 16, 2013 at 17:41

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.