我正在尝试使用ajax将数据发送到wordpress中的函数,但它没有发送正确的值。
这是我的 html,
<select id="make" name="make" class="form-control g-py-12 g-rounded-30">
<?php if(ICL_LANGUAGE_CODE=='en'): ?>
<option selected value="">Select Make</option>
<?php elseif(ICL_LANGUAGE_CODE=='ar'): ?>
<option selected value="">نوع السيارة</option>
<?php endif; ?>
<?php
/**
* Output is content
*/
$terms = get_terms(
array(
'taxonomy' => 'make',
'hide_empty' => false,
)
);
// Check if any term exists
if ( ! empty( $terms ) && is_array( $terms ) ) {
// Run a loop and print them all
foreach ( $terms as $term ) { ?>
<option value="<?php echo $term->name; ?>">
<?php echo $term->name; ?>
</option><?php
}
}
?>
</select>
这是我的 ajax 脚本
jQuery(document).ready(function() {
jQuery('select#make').on('change', function(){
var make = jQuery(this).val();
alert(make);
jQuery.ajax( {
url: 'https://uat.lumirental.com/wp-admin/admin-ajax.php?action=cms_formsubmit',
type: 'POST',
data: {'value' : 'test'},
contentType: false,
processData: false,
cache: false,
dataType: 'html',
success: function ( response ) {
alert("successfull");
},
error: function ( response ) {
alert( 'something went wrong' );
}
} );
});
});
这是我的函数.php函数
add_action( 'wp_ajax_cms_formsubmit', 'cms_formsubmit' );
add_action( 'wp_ajax_nopriv_cms_formsubmit', 'cms_formsubmit' );
function cms_formsubmit(){
echo $make = $_POST['value'];
print_r($_POST);
foreach($_GET as $key){
echo $key;
echo "<br/>";
}
$tt = array();
query_posts(array('post_type' => 'car_listing', 'tax_query' => array(
array(
'taxonomy' => 'make',
'field' => 'slug',
'terms' => $make,
)
) ) );
if ( have_posts() ) : while ( have_posts() ) : the_post();
echo $tt[] = '<option value="'.get_field('car_model').'"> '.get_field('car_model').' </option>' ;
endwhile;
endif;
return $tt;
}
当我尝试发送带有 post 的选择数据时,我几乎没有问题,它不会转到页面,但是当我使用它时,它没有发送我以后可以使用的适当值功能。 得到一些意外的数组,
需要删除以下内容
contentType : false,
processData: false,