admin管理员组文章数量:1122846
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
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
Share Improve this question edited Nov 8, 2018 at 21:55 Rob asked Nov 7, 2018 at 22:21 RobRob 111 silver badge4 bronze badges 5 |1 Answer
Reset to default 0Thanks 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
本文标签: plugin developmentajax multiple Values
版权声明:本文标题:plugin development - ajax multiple Values 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736285348a1927441.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
100200
. Is that not what you're getting? What is the message that you are getting in the log? – Jacob Peattie Commented Nov 8, 2018 at 7:36