我试图从一个选定的表单中传递一个变量,但无法使其工作-正确的方法是什么?
控制台现在给我对象错误:statusText: "parsererror"
Update:
更改了json\\u编码并更新了脚本。我知道获取correct值,但if/else语句没有触发。
Form
<select id="dropdown_shop_order_language" name="wcml_shop_order_language">
<option value="nl">Nederlands</option>
<option value="en" selected="selected">Engels</option>
<option value="de">Duits</option>
</select>
Javascript
jQuery(document).ready(function($) {
jQuery(\'#dropdown_shop_order_language\').on(\'change\', function(){
$.ajax({
dataType: \'json\',
type: "POST",
url: ajaxurl,
data: {
\'action\':\'my_action\',
\'dropdown_shop_order_language\': $(\'#dropdown_shop_order_language\').val()
},
success:function(data) {
console.log(data);
},
error: function(errorThrown){
console.log(errorThrown);
}
});
});
});
PHP
function my_enque_action( $hook ) {
global $post;
$postid = $post->ID;
if ( $hook == \'post-new.php\' || $hook == \'post.php\' ) {
if( get_post_type( $postid ) === \'shop_order\' ) {
wp_enqueue_script( \'lang_script\', get_template_directory_uri() . \'/assets/javascripts/language.js\', array( \'jquery\' ), \'1.0.0\', true );
}
}
}
add_action( \'admin_enqueue_scripts\', \'my_enque_action\', 10, 1 );
function my_action( ) {
$dropdown_shop_order_language = $_POST[\'dropdown_shop_order_language\'];
if ( $dropdown_shop_order_language == \'nl\' ) {
} elseif ( $dropdown_shop_order_language == \'de\' ) {
} elseif ( $dropdown_shop_order_language == \'en\' ) {
}
echo json_encode($dropdown_shop_order_language);
wp_die();
}
add_action(\'wp_ajax_my_action\', \'my_action\');
add_action(\'wp_ajax_nopriv_my_action\', \'my_action\');