Steve Kim
Steve Kim

Reputation: 5611

Sending php value to js via ajax

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;
});

Upvotes: 1

Views: 65

Answers (2)

Tristan
Tristan

Reputation: 3321

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;
});

Upvotes: 1

dranieri
dranieri

Reputation: 199

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;

Upvotes: 1

Related Questions