如果条件为PHP中的多个搜索输入

时间:2013-09-10 08:21:55

标签: php jquery wordpress search

我希望有一个简单的if条件来搜索多个输入(普通文本输入)和下拉类别框。我需要根据搜索的基础来确定条件。我需要一次从一个输入中的两个输入中搜索。如果两者都给出,我需要给其中一个优先于另一个。

    <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="Kodak Gallery">Kodak Gallery</option>  
    <option value="Ritzpix">Ritxpix</option>  
    <option value="PhotoShelter">PhotoShelter</option> 
    <option value="ScanDigital">ScanDigital</option> 
</select>  

    <input type="submit" id="searchsubmit" value="Search" align="right"/>           
        </form>

我有两个输入。

我想我必须这样做:

<?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']
         }
} ?>

我做得对吗?

1 个答案:

答案 0 :(得分:0)

<?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
      }
   }

?>