Not getting output on template after Ajax request
-
I’m really stumped and hoping someone can help.
Basicly what I’m trying to do is get the content of a div changed depending on which button you press.
I have 4 buttons and 1 div for the dynamic content. See my testpage here: https://www.pixelein.nl/testpagina/
At the moment you see all the possible content in the dynamic content section. The dynamic content should appear below this default content (placed for comparison).I got it far enough that the Ajax response is up and running. If you click on button three, you see the Ajax response showing the correct dynamic content.
However, I cannot figure out why the dynamic content isn’t loading into the div.My jQuery code in footer.php:
jQuery('.but').click(function(){ if(jQuery(this).hasClass('one')) { var data = { action: 'view_portfolio', galleryID: 1 }; jQuery.post(ajaxurl, data, function(response) { //alert('Got this from the server: ' + response); }); } else if(jQuery(this).hasClass('two')) { var data = { action: 'view_portfolio', galleryID: 2 }; jQuery.post(ajaxurl, data, function(response) { //alert('Got this from the server: ' + response); }); } else if(jQuery(this).hasClass('tre')) { var data = { action: 'view_portfolio', galleryID: 3 }; jQuery.post(ajaxurl, data, function(response) { alert('Got this from the server: ' + response); }); } else { var data = { action: 'view_portfolio', galleryID: 4 }; jQuery.post(ajaxurl, data, function(response) { //alert('Got this from the server: ' + response); }); } return false; });
My code in my themes functions.php:
add_action( 'wp_ajax_view_portfolio', 'view_portfolio' ); add_action( 'wp_ajax_nopriv_view_portfolio', 'view_portfolio' ); function view_portfolio(){ // look up for the path if ( !defined('ABSPATH') ) require_once( dirname(__FILE__) . '/../ngg-config.php'); global $wpdb; $ngg_options = get_option ('ngg_options'); $siteurl = site_url(); // get the gallery id $galleryID = intval( $_POST['galleryID'] ); if ($galleryID == null) { $galleryID = 1; } else { $galleryID = intval( $_POST['galleryID'] ); } // get the pictures if ($galleryID == 0) { $thepictures = $wpdb->get_results("SELECT t.*, tt.* FROM $wpdb->nggallery AS t INNER JOIN $wpdb->nggpictures AS tt ON t.gid = tt.galleryid WHERE tt.exclude != 1 ORDER BY tt.{$ngg_options['galSort']} {$ngg_options['galSortDir']} "); } else { $thepictures = $wpdb->get_results("SELECT t.*, tt.* FROM $wpdb->nggallery AS t INNER JOIN $wpdb->nggpictures AS tt ON t.gid = tt.galleryid WHERE t.gid = '$galleryID' AND tt.exclude != 1 ORDER BY tt.{$ngg_options['galSort']} {$ngg_options['galSortDir']} "); } if (is_array ($thepictures)){ foreach ($thepictures as $picture) { echo $picture->filename; echo ' - '; echo $siteurl."/".$picture->path."/".$picture->filename; echo '<br/>'; } } echo $thepictures; //die(); //exit; }
on the template page:
<div id="view_portfolio"> <div class="but one">knop 1</div> <div class="but two">knop 2</div> <div class="but tre">knop 3</div> <div class="but qua">knop 4</div> </div> <div id="fotos"> <?php view_portfolio(); ?> <br /> --------- <br /> <br /> <?php view_portfolio($thepictures) ?> </div>
- The topic ‘Not getting output on template after Ajax request’ is closed to new replies.