Conditional check for search results

你离开我真会死。 提交于 2019-12-13 06:31:43

问题


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.

  1. a normal search code (which searches for inputs which we provide)
  2. 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 -

  1. 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.

  2. 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)

  3. 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 the last part is the javascript code which helps to pass the category value which is selected using the dropdown menu.

will the below code work?

<?php
              if (!empty($_POST['search'])) { //if there is something in search_text
     if (empty($_POST['searchform']))  {
          //search by text
       } else {
          //combined text and category search
        }
} else  { //if there is nothing in search_text
       if (!empty($_POST['category']))  {
          // search by category
         }  else  {
          // error - no data in either $_POST['search'] or $_POST['category']
         }
} ?>

来源:https://stackoverflow.com/questions/18712609/conditional-check-for-search-results

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!