Support » Plugin: Ultimate WP Query Search Filter » Populate dropdown with ajax revisited

  • Previous topic here: https://wordpress.org/support/topic/populate-dropdown/

    So, I have to create a filter that is capable of filtering cars according to make and model.

    I have set up a custom post type called Vehicles (slug vehicles)
    Inside vehicles I have Category where model info is stored.
    – Manufacturer (BMW)
    — model (3.series)

    Then I have custom taxonomy, Manufacturer that holds all vehicle manufacturers.
    – Ford
    – BMW etc.

    In my plugin settings I have Taxonomy field (manufacturer) and category field (models) that I can use for filtering.

    Now, it all works with one word manufacturer like BMW or Ford but If i use two or more word car manufacturer like Mercedes-Benz or Land rover it does not find correct subitems for it and therefore returns all items.

    The problem is within ajax function found in previous thread that I linked top of this thread.

    I have modified it a bit to use more id-s than values/slugs.

    Again, it all works with one-word names like BMW, FORD etc. What am I missing, where is the problem?

    Ajax part:
    function ajax_uwpqsf() {
    $cat_id = get_cat_ID( ucfirst($_POST[‘id’]) );
    //echo $cat_id;
    $args = array( ‘parent’ => $cat_id, ‘orderby’ => ‘name’, ‘hide_empty’ => 1 );
    $categories = get_terms( ‘category’,
    array(
    ‘hide_empty’ => 1,
    ‘child_of’ => $cat_id,
    ‘parent’ => $cat_id,
    ‘hierarchical’ => false
    )
    );
    $modeldata = ”;
    foreach( $categories as $category ) {
    $modeldata .= ‘<option value=”‘.$category->term_id.'”>’.$category->name.'</option>’;
    }
    echo ‘<option value=”uwpqsftaxoall”>Mudel</option>’;
    echo $modeldata;
    die();
    }

    JQuery part:

    $(‘#tdp-0’).change(function(e){
    var cat_id = $(this).val();
    $.ajax({
    cache: false,
    type: ‘POST’,
    url: ajax_uwpqsf_object.ajaxurl,
    data: {
    ‘action’: ‘ajaxuwpqsf’,
    ‘id’: cat_id
    },
    dataType: “html”,
    success: function(data){
    $(‘#tdp-1’).html(data);
    $(‘#uniform-tdp-1 span’).html(data);
    console.log( data );
    }
    });
    e.preventDefault();
    return false;
    });
    $(“#tdp-1”).attr(‘disabled’,’disabled’);

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author TC.K

    (@wp_dummy)

    In this line:
    $modeldata .= ‘<option value=”‘.$category->term_id.'”>’.$category->name.'</option>’;
    try change term_id to slug.

    eg:
    $modeldata .= ‘<option value=”‘.$category->slug.'”>’.$category->name.'</option>’;

    Sorry, this still does not solve it. The problem is in ajax query rather than displaying option items.
    I have troubleshooted a bit the best I can. If user selects some category it echoes out to console. And if land rover or any other two word category is selected, it echoes out 0 as if it does not find that category(but it is there!).

    Filter part still works, but that ajax option population does not.

    Plugin Author TC.K

    (@wp_dummy)

    First, the category->term_id and category->slug is completely two different field in the table. In this plugin, it is using the slug for the query. So, yes it does matter.

    Secondly, in your ajax it the action object isajaxuwpqsf, wheres in your ajax function isajax_uwpqsf()` , it might be typo or something.

    third, this line var cat_id = $(this).val(); actually get the cat slug. If you want to use get_cat_ID() function, then the parameter that accept is the category name. So, you probably need to use text() instead of val(); eg: var cat_id = $(this).text();.
    And I think this is the main reason. Because, when you use the $(this).val() it will get the slug, where Land Rover will become land-rover, and it will leads to get_cat_ID() not getting any result because it only accept <a href=”https://codex.wordpress.org/Function_Reference/get_cat_ID”>category NAME</a> So try to get the category name (the text that we see in the dropdown, not the actual value of the selected field).

    Note that, var cat_id = $(this).text();. I have not test this. Try google if it is not working.

    • This reply was modified 3 years ago by TC.K.
    • This reply was modified 3 years ago by TC.K.

    Ok, this was a stupid mistake not understanding the code completely.

    My solution is this:

    
    function ajax_uwpqsf() {
    	$cat_id2 = ucfirst($_POST['id']);
    
    	  $idObj = get_category_by_slug( $cat_id2 ); 
    	  $idd = $idObj->term_id;
    
    	$args = array( 'child_of' => $idd, 'orderby' => 'name', );
    	$categories = get_terms( 'category', $args );
    	$modeldata = '';
    	foreach( $categories as $category ) {
    		$modeldata .= '<option value="'.$category->slug.'">'.$category->name.'</option>';
    	}
    	echo '<option value="uwpqsftaxoall">Mudel</option>';
    	echo $modeldata;
    	die();
    }
    
    • This reply was modified 3 years ago by VampKALLE. Reason: Removed unneccesary code

    I am new to WP and have been trying to accomplish a similar thing using a plugin.
    I am able to get first parent “Models” drop dow to work and pulll values from DB, I can tell that I am accessing the .change call in the script because the alert works but I can’t seem to get the post to work. Any idea or suggestion is greatly appreciated. the entire solution works outside WP fine but not in WP.

    This is my script
    ==================================================================
    <script src=”http://localhost/tmwp/jquery.min.js”></script&gt;
    <script>
    jQuery(document).ready(function(){
    alert(‘test1’);
    });
    jQuery(document).ready(function(){

    $(‘#make’).change(function(){
    var make_name = $(this).val();
    if(make_name){ alert(make_name + ‘ : on_change call invoked’);
    $.post( “http://localhost/tmwp/ajaxData2.php&#8221; );
    $.ajax({
    type:’POST’,
    url:’http://localhost/tmwp/ajaxData2.php&#8217;,
    data:’make_name=’+make_name,
    success:function(html){
    $(‘#model’).html(html);
    }
    });
    }else{
    $(‘#model’).html(‘<option value=””>Select Make first</option>’);
    }
    });

    });
    </script>
    =============================================================================

    ajaxData2.php file content:
    =========================================================================
    <?php
    //Include database configuration file
    global $wpdb;

    //if(isset($_POST[“make_name”]) && !empty($_POST[“make_name”])){

    $make= $_POST[“make_name”];
    echo “$make”;
    $models = $wpdb->get_results( “SELECT model_name FROM wp_models WHERE make_name = ‘$make’ ORDER BY model_name ASC”, ARRAY_A);

    if(count($models) > 0){

    echo ‘<option value=””>Select Model</option>’;
    foreach($models as $row){
    $modelName=$row[‘model_name’];
    echo ‘<option value=”‘.$modelName.'”>’.$modelName.'</option>’;
    }
    }else{
    echo ‘<option value=””>Model not available</option>’;
    }
    //}

    ?>

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Populate dropdown with ajax revisited’ is closed to new replies.