I built a custom form
for search filtering on my site. I'm using hidden input fields to decide what type of search filter was selected. The value gets passed and I check on my search page what the value was, and then direct to the correct search results page like so:
<?php
$search_refer = $_GET['search_type'];
if($search_refer == 'members') {
load_template(TEMPLATEPATH . '/search-members.php');
} else if($search_refer == 'event') {
load_template(TEMPLATEPATH . '/search-member-event.php');
} else {
load_template(TEMPLATEPATH . '/search-site.php');
}
?>
Here are the two fields in HTML/PHP
<div class="state"> // Sorting by user State
<input type="hidden" name="search_type" value="members">
<select id="stateDrop" name="state">
<option value="">State</option>
<?php
foreach($states as $state) {
echo '<option value="'.$state.'">'.$state.'</option>';
}
?>
</select>
</div>
<div class="sort-event"> // Sorting by an Event
<input type="hidden" name="search_type" value="event">
<select id="eventDrop" name="event">
<option value="">Event</option>
<?php
$args = array(
'post_type' => 'events',
'order' => 'ASC'
);
$eQuery = new WP_Query( $args );
while( $eQuery->have_posts() ) : $eQuery->the_post();
echo '<option value="'.$eQuery->post->post_title.'">'.$eQuery->post->post_title.'</option>';
endwhile;
wp_reset_query();
?>
</select>
</div>
Both have the name search_type
as a hidden name, but the values are different. members
vs event
. My problem is when you click on the submit button, the value being received is always the last hidden input value. e.g. event
.
Is there a way to decide which field was chosen so that I can then direct the search results to the correct page. (the search pages pull in different information based on the search).
Thanks,
As you have two inputs with the same name, only one of them will ever be set to the server.
If you want them to use the same name you're going to need to use POST and two forms, which would require two separate submit buttons but go to the same page. This way you can submit the desired form based on the filter.
<form action="yourpage.php" method="post">
<input type="hidden" name="search_value" value="members"/>
... member stuff
<input type="submit" value="Filter Members"/>
</form>
<form action="yourpage.php" method="post">
<input type="hidden" name="search_value" value="event"/>
... event stuff
<input type="submit" value="Filter Events"/>
</form>
Or you could use an input type of a radio button, as that supports having the same name as only one value can be selected at once.
Event: <input type="radio" name="search_type" value="event"/>
Members: <input type="radio" name="search_type" value="members"/>
// Then in PHP $_POST["search_type"] will hold the selected value.
If you wish to stick with GET, you could use JavaScript to set the value of a single hidden element before the user clicks the link.