组合文本框和复选框以创建搜索框

Combining a text box and a check box to create a search box

我想为我的站点设置搜索 textboxcheckbox 过滤器。搜索将搜索地点和位置的名称,而 checkbox 将过滤 activity.

现在我有一个可用的搜索 textbox 和一个可用的 checkbox 过滤器,但无法弄清楚 如何组合它们

我非常感谢任何帮助或教程链接等,因为我仍在学习这两种语言并且最近才开始苦苦挣扎,现在完全停滞不前。

多亏了 kstro21,代码才能正常工作!

这是 PHP

<?php
  if(isset($_POST['submit'])){
  if(isset($_GET['go'])){
  if(preg_match("/^[  a-zA-Z]+/", $_POST['name'])){
  $name=$_POST['name'];
  //connect  to the database
  $db=mysql_connect  ("host", "dbo560757688",  "pw") or die ('I cannot connect to the database  because: ' . mysql_error());
  //-select  the database to use
  $mydb=mysql_select_db("db560757688");
 // Fruits
$fruits = array();
foreach($_POST['fruit'] as $fruit) {
    $fruit = mysql_real_escape_string($fruit);
    $fruits[] = "'{$fruit}'"; 
}
    //-query  the database table
  $sql="SELECT * FROM Contacts WHERE Activity IN (" . implode(", ", $fruits) . ") AND FirstName LIKE '%" . $name .  "%' OR LastName LIKE '%" . $name ."%'";
  //-run  the query against the mysql query function
  ?>
  <?php 
//omitted code

$result=mysql_query($sql);

//close your php just after this
  }
  }
  }
?>
<table border="1">
    <thead>
        <tr>
          <th>Name</th>
          <th>Last Name</th>
          <th>Activity</th>
        </tr>
    </thead>
    <tbody>
        <?php 
        //-create  while loop and loop through result set
          while($row=mysql_fetch_array($result)):
          $FirstName=$row['FirstName'];
          $LastName=$row['LastName'];
          $Activity=$row['Activity'];
          $Email=$row['Email'];
          //-display the result of the array
          ?>
        <tr>
            <td><?php echo $FirstName ?></td>
            <td><?php echo $LastName?></td>
            <td><?php echo $Activity ?></td>
        </tr>
      <?php endwhile; ?>
    </tbody>
</table>

HTML 代码

    <form  method="post" action="search.php?go"  id="searchform">
      <input  type="text" name="name">
      <br /> 
          <input type="checkbox" name="fruit[]" value="Paintball" /> 
          Banana<br />
    <input type="checkbox" name="fruit[]" value="Swimming Pool" /> Apple
    <br />
    <input type="checkbox" name="fruit[]" value="Gym" /> Peach
    <br />
      <input  type="submit" name="submit" value="Search">
    </form>
</form> 

您使用 php 打印的 html 代码有一些问题,例如, table 标签未关闭,与第一个 tr 相同,另外,所有的代码都在一个 for 中,所以它会被打印多次,是你想要的吗?

首先,我建议您在 $fruits[] = "'{$fruit}'"; 之后尝试关闭 for 循环,然后再试一次

编辑:
我认为你的 sql 查询是错误的,更改

WHERE Activity LIKE (" . implode(", ", $fruits) . ")";

用于:

WHERE Activity IN (" . implode(", ", $fruits) . ")";

您不能使用带有逗号分隔值的 LIKE,而是使用 IN

你可以在同一个文件中混合 html 和 php 代码,这样你就可以做出这样的东西

<?php 
//...omitted code

$result=mysql_query($sql);

//close your php just after this
?>
<table border="1">
    <thead>
        <tr>
          <th>Name</th>
          <th>Last Name</th>
          <th>Activity </th>
        </tr>
    </thead>
    <tbody>
        <?php 
        //-create  while loop and loop through result set
          while($row=mysql_fetch_array($result)):
              $FirstName=$row['FirstName'];
              $LastName=$row['LastName'];
              $Activity=$row['Activity'];
              $Email=$row['Email'];
              //-display the result of the array
          ?>
            <tr>
                <td><?php echo $FirstName ?></td>
                <td><?php echo $LastName ?></td>
                <td><?php echo $Activity ?></td>
            </tr>
      <?php endwhile; ?>
    </tbody>
</table>