1

So, I have the following ajax call:

jQuery.ajax({
    type: "GET",
    url: custom.ajax_url, 
    dataType: 'html',
    data: ({ action: 'ajax_call'}),
    success: function(data){    
            jQuery('.container').append(data);// Need changes

});

Then my php:

function rhmain_tag() {
    // 1. Getting wp tag list:
    $args = array(
        'orderby' => 'count',
        'order'   => 'DESC'
    );

    $tags = get_tags($args);
    foreach ( $tags as $tag ) {
        echo $tag->term_id;
    }

    //2. Getting "another.php" file
    $template_part_path = 'page-parts/another_php';
    get_template_part($template_part_path);

    exit;
}

add_action('wp_ajax_ajax_call', 'ajax_call');
add_action('wp_ajax_nopriv_ajax_call', 'ajax_call');

As you can see, in the php function, there are two separate thing going on.

  1. Getting tag list passed onto the js as variable.
    • echo $tag->term_id; shows a number like this "16508164981650616502165031650416505165071650116520." So, somehow I need to put comma in between each term id (not sure how) so it looks like this : "16508,16498,16506,16502,16503,16504,16505,16507,16501,16520"
    • Then I need to pass these values to js to be used as a variable (not sure how)
  2. another_php file is passed onto js (Works fine).

Questions:

  1. How to do I add "comma" in between the tag term_id?
  2. How do I pass these tag values to js?

EDIT: Using json_encode to pass data

PHP

function rhmain_tag() {     
$template_part_path = 'page-parts/another_job';
$return_data['another_job'] = get_template_part($template_part_path);
$return_data['tag_id'] = '16498';
echo json_encode($return_data);
exit;
}
add_action('wp_ajax_rhmain_tag', 'rhmain_tag');
add_action('wp_ajax_nopriv_rhmain_tag', 'rhmain_tag');

JS:

jQuery.ajax({
    type: "GET",
    url: custom.ajax_url, 
    dataType: 'html',
    data: ({ action: 'ajax_call'}),
    success: function(data){    
            jQuery('.container').append(data.another_php);
            var tag_list = data.tag_id;
});
5
  • What is passed to javascript from the ajax call is whatever you echo out. Commented Dec 16, 2015 at 23:49
  • On the echo you can concatenate a comma echo $tag->term_id.',';. Do you want to echo the IDs as well as the template you are appending to .container? It sounds like you are intending to use the IDs in jquery, so you probably don't want them appended to .container? Commented Dec 16, 2015 at 23:51
  • @Tristan. You are exactly right. I want to use the id separately from the php file. How do I separate these two different values (another_php.php and ids). For the ids, I am planning to use them in a variable for another function. Commented Dec 16, 2015 at 23:55
  • To use them separately either use 2 ajax calls, OR rather than echoing the outputs directly in the php script, use json_encode() an array containing the IDs and the another_php, then use them separately in the js. Commented Dec 17, 2015 at 0:00
  • I added an edited version in the question using json_encode. Could you take a look at it? Commented Dec 17, 2015 at 0:08

2 Answers 2

1

Using json_encode to send an array containing both data that you require front end you then need to make the following changes to your ajax function.

  1. Change the dataType to json
  2. Access the data as data.another_php and data.tag_id

The full ajax:

jQuery.ajax({
    type: "GET",
    url: custom.ajax_url, 
    dataType: 'json',
    data: ({ action: 'ajax_call'}),
    success: function(data){    
        jQuery('.container').append(data.another_php);
        var tag_list = data.tag_id;
});
Sign up to request clarification or add additional context in comments.

Comments

1

To add the comma in between you could use a variable to store instead of echo'ing immediately. Like this :

$args=array(
    'orderby'=>'count',
    'order'=>'DESC'     
);
$tags = get_tags($args);
$tag_list = "";
foreach ( $tags as $tag ) {
    $tag_list += $tag->term_id + ",";
}
// Now trim the last comma
$tag_list = rtrim($tag_list, ",");
echo $tag_list;

1 Comment

I used @tristan's answer for the comma issue : echo $tag->term_id.',';

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.