【发布时间】:2015-05-05 23:23:01
【问题描述】:
我想为我的网站搜索textbox 和checkbox 过滤器。搜索将搜索地点和位置的名称,而 checkbox 将过滤活动。
现在我有一个有效的搜索 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>
【问题讨论】:
-
您的代码看起来不错,您是否遇到任何错误?你的表单 html 是什么?
-
现在将使用我的 HTML 进行更新,我没有收到任何错误,只是没有显示。
-
如果你 var_dump 它包含任何内容吗? $fruits[] = "'{$fruit}'";