Tell me more ×
WordPress Answers is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am trying to create a custom search box and would like to know how to check multiple condition based on different inputs.There are 2 inputs.

a normal search code (which searches for inputs which we provide) a category dropdown menu (which searches according to the name of the category) I would like a different condition check for each of the foll scenarios -

The search button must be able to pick up either the search string or the category selected from the dropdown menu and then deliver the search results.

If both are provided (meaning that someone types a name in the search box and also select a category) - the search keywords in the box must take precedence and the results should show up for that string (not the category page)

If the category is selected but nothing is typed in the search box; then that respective category page must be displayed.

OK, hope that explains what I am trying to do. So here is the code I have so far:

<form role="search" method="get" id="searchform" action="http://www.sitename.com/">
<label class="screen-reader-text" for="s">
</label>
<input type="text" value="" name="s" id="s" align="right" />
<label for="category">Select Your Category</label>
<input class="selectcategorydropdown" type="hidden" id="category" />
<select name="s" id="category" onchange="getSelectedValue();">
    <option value="">none</option>
    <option value="cat1">category1</option>
    <option value="cat2">category2</option>
    <option value="cat3">category3</option>
    <option value="cat4">category4</option>
</select>
<input type="submit" id="searchsubmit" value="Search" align="right" />
</form>

<script type="text/javascript">
    function getSelectedValue() {
        var index = document.getElementById('category').selectedIndex;
        ("value=" + document.getElementById('category').value);
        ("text=" + document.getElementById('category').options[index].text);
    }  
</script>

and php code -

<?php

   // Check if form was submitted
   if(!isset($_POST['search'])
   {
      // Display the form
   }
   else
   {
      // Form was submitted, check if values are empty
      if(trim($_POST['category1'])=="" )
      {
         // One or more value is empty, do something
      }

      else if (trim($_POST['category2'])=="")
      {
         // Process form
      }
   }

?>

i need to connext both inputs and scenarios. thanks in advance. also i am new in this field. please pardon me if i am asking some irrelevant questions

share|improve this question

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.