【问题标题】:ajax is not submiting proper value with GET method and with POST always blank,ajax 未使用 GET 方法提交正确的值,并且 POST 始终为空白,
【发布时间】:2020-05-15 20:45:05
【问题描述】:

我正在尝试使用 ajax 在 word press 中将数据发送到我的函数,但它没有发送正确的值。

这是我的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' );
            }
        } );

   });

   });  

这是我的function.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 发送选择数据时,我遇到了一些问题,它不会转到页面,但是当我让它工作但它没有发送正确的值时,我可以稍后在函数中使用它。得到一些意想不到的数组,

【问题讨论】:

  • 你能死吗();请问?
  • 我为什么要把它加到死,??
  • 删除 contentTypeprocessData。不发送 FormData 对象时不需要这些

标签: php jquery html ajax wordpress


【解决方案1】:

需要删除关注

contentType : false,
processData: false,

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-04-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多