Im trying to pass a variable from a select form, but cant get it to work - whats the correct way to do this?
Console is now giving me object error: statusText: "parsererror"
Update:
Changed the json_encode and updated the script. I know get the corretc value, but the if/else statement isnt firing.
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');
Im trying to pass a variable from a select form, but cant get it to work - whats the correct way to do this?
Console is now giving me object error: statusText: "parsererror"
Update:
Changed the json_encode and updated the script. I know get the corretc value, but the if/else statement isnt firing.
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');
You seem to be missing the ajax actions, there are two, one for logged in users and one for not logged in users.
Going by your action you will need to add the following actions.
// this one for logged in users
add_action('wp_ajax_my_action', 'my_action');
// this one for not logged in users
add_action('wp_ajax_nopriv_my_action', 'my_action');