I’m trying to construct a search form and make a search on wordpress getting the values from two select elements from within the form. Anyone know how? Do I have to use JS, AJAX or both? Any help would be appreciated.
function categories_header_form()
{
?>
<div id="header-form">
<h3 class="form-title">
<?php echo 'ÎναζήÏηÏη ÏÏοÏÏνÏÏν ανά ÏεÏιοÏή' ?>
</h3>
<form id="search-form" action="<?php bloginfo('url'); ?>" method="get" >
<div class="form-container">
<?php nomoi(); ?>
<?php products_selection(); ?>
<button type="submit" class="button" id="search-form-button">ÎÏÏεÏη</button>
</div>
</form>
</div>
<?php
}
function products_selection()
{
$args = array(
'post_type' => 'seller',
'taxonomy' => 'category',
'hide_empty' => 0,
'exclude' => 1,1078,1079
);
$products = get_categories( $args );
if ( $products ) {
echo '<select id="products-select">';
echo '<option selected="" disabled="" value="0"><span>Î ÏοÏÏνÏα</span></option>';
foreach ($products as $product) {
echo '<option class="product-name" id="'. $product->term_id .'">'. $product->name .'</option>';
}
echo '</select>';
}
}
function nomoi()
{
$args = array(
'post_type' => 'seller',
'taxonomy' => 'nomos',
'hide_empty'=> 0,
'parent' => 0
);
$categories = get_categories( $args );
if ( $categories ) {
echo '<select id="nomoi-select" name="nomoi">';
echo '<option selected="selected" disabled="disabled"><span>Îομοί</span></option>';
foreach ( $categories as $category ) {
$id = $category->term_id;
$name = $category->name;
echo '<option class="nomos" id="'. $id .'">'. $name .'</option>';
}
echo '</select>';
echo '<select id="town-select" name="towns">';
echo '<option class="town-disabled" selected="selected" disabled="disabled"><span>Î ÏλειÏ</span></option>';
echo '</select>';
}
}
This is my search form along with the select tags. I’m a starter in JS and AJAX and don’t know how to perform the query correctly. The second select field is populated through AJAX
Here is one way you could do it. Listen for changes to the select elements and save the value. Then use the values in the for the search query.