admin管理员组文章数量:1325236
I have a html form with select boxes and I want to display different data depending on what the users select.
All I really need to do is change 1 variable in php based on the selection.
I make an external API call that returns data and displays it in an html table. I want to to change 1 small part of the api call (an id in the get uri) depending on the users choice.
Is there a simple way to do this?
I've got as far as making the jquery and enqueuing it with wordpress, and I'm getting a 200 response when changing the select box value. But I just can't seem to be able to change the PHP.
To avoid bombarding with code, I've changed the callback function to give an examlple of what I'd like to happen. Greatly appreciate any feedback. Thanks.
HTML
<form class="choose-site" action="" method="post">
<select id="choose-site" name="site-choice">
<option value="site1"><?php echo $primary_site ?></option>
<option value="site2"><?php echo $site_2 ?></option>
</select>
</form>
<div id="test123">
<p><?php echo $msg; ?></p>
</div>
JQUERY
(function($) {
$(document).ready(function() {
$("#choose-site").on("change",function() {
const val = this.value;
if (val) {
$.ajax({
type : "post",
dataType : "JSON",
url: my_ajax_object.ajax_url,
data : {
action: "my_ajax_action"
},
success: function(data) {
$("#test123").html(data);
}
});
}});
});
})(jQuery);
PHP in Functions.php
<?php
add_action( 'wp_ajax_my_ajax_action', 'users_details_callback' );
add_action( 'wp_ajax_nopriv_my_ajax_action', 'users_details_callback' );
function users_details_callback() {
if (isset($_POST['site-choice']) && $_POST['site-choice'] == 'site1') {
$msg = "hi";
}
if (isset($_POST['site-choice']) && $_POST['site-choice'] == 'site2') {
$msg = "bye";
}
wp_die();
return $msg;
}
?>
Is there a way to change the value of $msg and send it back to the HTML?
I have a html form with select boxes and I want to display different data depending on what the users select.
All I really need to do is change 1 variable in php based on the selection.
I make an external API call that returns data and displays it in an html table. I want to to change 1 small part of the api call (an id in the get uri) depending on the users choice.
Is there a simple way to do this?
I've got as far as making the jquery and enqueuing it with wordpress, and I'm getting a 200 response when changing the select box value. But I just can't seem to be able to change the PHP.
To avoid bombarding with code, I've changed the callback function to give an examlple of what I'd like to happen. Greatly appreciate any feedback. Thanks.
HTML
<form class="choose-site" action="" method="post">
<select id="choose-site" name="site-choice">
<option value="site1"><?php echo $primary_site ?></option>
<option value="site2"><?php echo $site_2 ?></option>
</select>
</form>
<div id="test123">
<p><?php echo $msg; ?></p>
</div>
JQUERY
(function($) {
$(document).ready(function() {
$("#choose-site").on("change",function() {
const val = this.value;
if (val) {
$.ajax({
type : "post",
dataType : "JSON",
url: my_ajax_object.ajax_url,
data : {
action: "my_ajax_action"
},
success: function(data) {
$("#test123").html(data);
}
});
}});
});
})(jQuery);
PHP in Functions.php
<?php
add_action( 'wp_ajax_my_ajax_action', 'users_details_callback' );
add_action( 'wp_ajax_nopriv_my_ajax_action', 'users_details_callback' );
function users_details_callback() {
if (isset($_POST['site-choice']) && $_POST['site-choice'] == 'site1') {
$msg = "hi";
}
if (isset($_POST['site-choice']) && $_POST['site-choice'] == 'site2') {
$msg = "bye";
}
wp_die();
return $msg;
}
?>
Is there a way to change the value of $msg and send it back to the HTML?
Share Improve this question asked Aug 22, 2020 at 9:45 dylzeedylzee 1351 silver badge6 bronze badges 3 |1 Answer
Reset to default 0Is there a way to change the value of
$msg
and send it back to the HTML?
Your code is basically already doing that, but there are a few issues:
Your AJAX JS (
jQuery.ajax()
) is not sending the POST data namedsite-choice
to your AJAX PHP callback (users_details_callback()
) which reads the data via$_POST['site-choice']
.Your AJAX PHP callback would result in WordPress echoing a zero (
0
) because you're callingwp_die()
before returning any output and even if you returned it beforewp_die()
is called, you should actuallyecho
the output and notreturn
it.Your AJAX JS is expecting a JSON data back from the server (because you set
dataType
toJSON
—dataType : "JSON"
), so your AJAX PHP callback should return a valid JSON response.
So to fix the issues:
In your PHP function (
users_details_callback()
), you can usewp_send_json_success()
to send a JSON response without having to callwp_die()
: (note that I also optimized theif
condition)function users_details_callback() { if ( isset( $_POST['site-choice'] ) ) { $msg = ''; if ( $_POST['site-choice'] == 'site1' ) { $msg = 'hi'; } elseif ( $_POST['site-choice'] == 'site2' ) { $msg = 'bye'; } wp_send_json_success( $msg ); } // Send an error if we receive an invalid data. wp_send_json_error( 'your error' ); }
In your
jQuery.ajax()
(or$.ajax()
) call:$.ajax( { ... data: { action: 'my_ajax_action', // Send the site-choice data. 'site-choice': $( '#choose-site' ).val(), }, success: function( data ) { // 'data' is now an object and the $msg value from the server is put in // data.data, so use that instead of just 'data'. $( '#test123' ).html( data.data ); }, } );
本文标签: theme developmentHow to change PHP variables with AJAX request in WordPress
版权声明:本文标题:theme development - How to change PHP variables with AJAX request in WordPress 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742168055a2426197.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
wp_die()
, but by "return", I mean echo the response, soecho $msg; wp_die();
. Then your JSsuccess
callback would receive the$msg
value in thedata
variable. However, if you set thedataType
tojson
, then you should echo a valid JSON string. – Sally CJ Commented Aug 22, 2020 at 9:59admin-ajax
" - by the REST API handbook. – Sally CJ Commented Aug 22, 2020 at 10:32