Solution 1 :

Need to remove following

contentType : false,
processData: false,

Problem :

I am trying to send data to my function in word press using ajax but its not sending it’s proper value.

Here is my 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>

here is my ajax Script

 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' );
            }
        } );

   });

   });  

here is my function.php function

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;
    }

I have few problems when i try to send select data with post it dont go to page, but when i do with get it working but it not sending proper value which i can use later on function. getting some unexpected array,

Comments

Comment posted by Neal Developer

can you die(); it please ?

Comment posted by Kamran Hassan

why should i add it to die, ??

Comment posted by Rory McCrossan

Remove

By