Viewing 1 reply thread
  • Author
    Posts
    • #7320
      cfuyuki
      Member

      Hi there,
      I have been trying to incorporate Ajax in shortcode with piklist however it doesn’t seem to work. May i ask if there is anything that i should be careful when using Ajax for shortcode with piklist? I have already made several similar Ajax shortcode without piklist, which is perfectly working. I reviewed many times whether there is something wrong with how i implemented Ajax on piklist shortcode feature but wasn’t able to find the reason. Any suggestion would be much appreciated.

      Here is the code:

      JS

      var parent = $('*[class*="ts-hello_world_ajax"]');
      
              $.each(parent, function (i, value) {
                  var item = $(this);
                  var button = item.find('.btn'),
                      callback = item.find('.callback');
      
                  button.on('click', function () {
                      var say = $(this).data('say');
                      //console.log(HWA.ajaxurl_piklist);
                      $.ajax({
                          url: HWA.ajaxurl_piklist,
                          type: 'post',
                          data: {
                              action: 'hello_world_ajax',
                              say: say
                          },
                          complete: function( xhr, status ){
                              console.log("Request complete: " + status);
                          },
                          success: function (response) {
                              console.log('Request success: ' + response);
                              callback.html(response);
                          },
                          error: function(error){
                              console.log(error);
                          }
                      });
                  });
              });

      PHP

      <?php
      /*
      Shortcode: hello-world-ajax
      */
      
      wp_localize_script('tspt-frontend', 'HWA', array(
      	'ajaxurl_piklist' => admin_url('admin-ajax.php')
      ));
      add_action('wp_ajax_hello_world_ajax', 'hwa_callback', 10, 999);
      add_action('wp_ajax_nopriv_hello_world_ajax', 'hwa_callback', 10, 999);
      
      global $post;
      $op = '';
      $op .= '<section class="hello-world ts-hello_world_ajax">';
      $op .= '<button class="btn btn-lg popover-button" data-say="' . $say . '"><i class="glyphicon glyphicon-repeat"></i></button>';
      $op .= '<p class="callback" style="min-height: 28px;">' . '' . '</p>';
      $op .= '</section>';
      ?>
      
      <em><?php echo $op; ?></em>
      
      <?php
      /**
       *
       * Function: hello-world-ajax_callback.
       * @return  ajax call back
       * @since  1.0.0
       *
       */
      function hwa_callback() {
      	$say = $_POST['say'];
      	if (defined('DOING_AJAX') && DOING_AJAX) {
      		echo $say;
      		wp_die();
      	} else {
      		wp_redirect(home_url());
      		exit();
      	}
      }

      Thank you in advance,

    • #7321
      cfuyuki
      Member

      By the way it only returns 0.

      Attachments:
      You must be logged in to view attached files.
Viewing 1 reply thread
  • You must be logged in to reply to this topic.