• 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>

Viewing 5 replies - 1 through 5 (of 5 total)
  • Try changing your jQuery to this (obviously untested as can’t access dynamic content):

    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) {
                    $('#fotos').html(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;
      	});

    Thread Starter Pixelein

    (@pixelein)

    Nope, no difference.

    Your still getting the alert message?

    Thread Starter Pixelein

    (@pixelein)

    No, but the proper content still isn’t being shown.
    The content the alert shows is the content I need placed on the page.

    Yes i understand that is the content!

    You now also have an extra }); line 366 ..

    Anyway my first suggestion works fine apart from I wrote $ instead of jquery !!! see:
    https://jsfiddle.net/c2pxr/6/
    obviously i cant access the ajax data so i substituted it!!!

    Here is the code again with jQuery instead of $:

    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) {
                    jQuery('#fotos').html(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;
      	});

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Not getting output on template after Ajax request’ is closed to new replies.