1

i tryed to return multiple Values from an Ajax Call,

I would Like to retun Value a and value b in two separat Variables in my Ajax function.

Is it posible to do that? or could I use an Array? Or should i do for every Call a own Function?

My Function: "here i will do one ore more Database querys"

function rob_ajax_vorlage() {
global $wpdb;
$a = "100";
echo $a;
$b = "200"; //second value that doesen work jet
echo $b; //second value that doesen work jet
wp_die(); // just to be safe
}
add_action( 'wp_ajax_rob_ajax_vorlage_approal_action', 'rob_ajax_vorlage' );
add_action( 'wp_ajax_nopriv_rob_ajax_vorlage_approal_action', 'rob_ajax_vorlage' );

And my Ajax call

jQuery(document).ready(function($) {
    $( '#rob-wp-ajax-vorlage-button' ).click( function() {
    var abc = $( '#abc' ).val();
    $.ajax({
    method: "POST",
    url: ajaxurl,
    data: { 'action': 'rob_ajax_vorlage_approal_action', 'abc': abc, }
    })
     .done(function( data ) {
            console.log('Successful AJAX Call! /// Return Data: ' + data);  
    .fail(function( data ) {
    console.log('Failed AJAX Call :( /// Return Data: ' + data);
  });
  });
  });

Im A Beginner so sorry if i asked something crazy.

so it would be grade if someone could help me to use return two values.

Thank you

Rob

5
  • 1
    Have you considered using the REST API instead of the old wp-admin AJAX?
    – Tom J Nowell
    Nov 7, 2018 at 22:31
  • The return data of your code will be 100200. Is that not what you're getting? What is the message that you are getting in the log? Nov 8, 2018 at 7:36
  • HI Tom, Now not jet. Do you have a Example for me? I would do get the retun in two variables not only in one?
    – Rob
    Nov 8, 2018 at 8:55
  • Hi Jacob, Yes i Get this return. But i Want to have to seperat variables? so the i can work with Value a and value b in two variables,, Hope you understand what i like to do?
    – Rob
    Nov 8, 2018 at 8:57
  • 1
    @Rob you need to return structured data, not a singular value, e.g. JSON. A quick google should lead to the instructions on building a primitive REST API endpoint, or a new question on this site. As for PDF creation, that's an entirely new question, and one that's not as easy as you might think ( and less of a WP question, more of a generic PHP/JS one too ). I'd also strongly suggest that you indent your code, it will eliminate entire classes of bugs, and make it easier to work with. Any half decent free editor will do the indenting for you automatically
    – Tom J Nowell
    Nov 8, 2018 at 16:27

1 Answer 1

0

Thanks to give me the advise to work with JSON.

I Think i get it now.

Perhaps someone could look over it?

Here My Code

Button:

<div id="div3">
<button id="rob-wp-ajax-vorlage-button">ok</button>
</div>

My PHP Code

function load_my_ajax_vorlage()
{
wp_enqueue_script( 'my_custom_js', plugins_url( 'ajax-vorlage-script.js',   __FILE__ ), array('jquery'), false, false );
}
add_action('wp_enqueue_scripts', 'load_my_ajax_vorlage');
add_action( 'wp_footer', 'load_my_ajax_vorlage' );

add_action('wp_enqueue_scripts', 'example_localize_ajax');

function example_localize_ajax(){
wp_localize_script('jquery', 'ajax', array(
    'url' => admin_url('admin-ajax.php'),
    'nonce' => wp_create_nonce('example_ajax_nonce'),
));
}


//Example AJAX Function
add_action('wp_ajax_example_function', 'example_function');
add_action('wp_ajax_nopriv_example_function', 'example_function');
function example_function(){

$var1 = "value1";
$var2 = "value2";
echo json_encode(array($var1, $var2));

wp_die(); // this is required to terminate immediately and return a  proper response:- https://codex.wordpress.org/AJAX_in_Plugins
}

my JS

var ajaxurl = '<?php echo admin_url("admin-ajax.php") ?>';
jQuery(document).ready(function($) {
$( '#rob-wp-ajax-vorlage-button' ).click( function() {

jQuery.ajax({
    type: "POST",
    url: ajax.url,
 dateType: 'JSON',
    data: {
        nonce: ajax.nonce,
        action: 'example_function',
        data: {
            //firstname: 'fname',
            //lastname: 'lname'
        },
    },
    success: function(response){
        console.log('Successful AJAX Call! /// Return Data: ' + response); 
        var result = $.parseJSON(response);     
        console.log('result0 ' + result[0]); 
        console.log('result1 ' + result[1]); 
         },
             error: function(XMLHttpRequest, textStatus, errorThrown){
             console.log('ERROR AJAX Call! /// Return Data: ' + XMLHttpRequest);  
             },
    timeout: 60000
});

return false;
    }); 
 });

I Delete the scentence with the PDF i think you are right.

I will have a Look in REST API...

is this the right way?

Thx Rob

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.