Jump to content

Multiple Search Options using radio buttons?


b

Recommended Posts

I've tried to post on a few other forums for information on how to write a very simple search script that will allow multiple search options to search a mysql database.  I have a script written that searches by a users last name, but I want to also offer the option to search by a users city or state.  I have a couple radio buttons in the html, but don't have any idea how to manipulate the php.  Any suggestions?  Here's the code I have: 

 

<?php

include('config.php'); 

$SQL_FROM = 'adr';
$SQL_WHERE = 'last';


?>
<?php
$searchq		=	strip_tags($_GET['q']);
$getRecord_sql	=	'SELECT * FROM '.$SQL_FROM.' WHERE '.$SQL_WHERE.' LIKE "'.$searchq.'%"';
$getRecord		=	mysql_query($getRecord_sql);
if(strlen($searchq)>0){
// ---------------------------------------------------------------- // 
// AJAX Response													// 
// ---------------------------------------------------------------- // 

echo '<ul>';
while ($list = mysql_fetch_array($getRecord)) {?>
	<li><a href="runners2.php?name=<?php echo urlencode($list['first'].' '.$list['last']); ?>"><?php echo $list['last']; ?> <small><?php echo $list['first']; ?></small><small><?php echo $list['city']; ?></small><small><?php echo $list['age']; ?></small></a></li>
<?php } 
echo '</ul>';
?>
<?php } ?>

 

The bottom portion is an AJAX response that makes an auto suggestion according to what letters you type.  Thanks in advance!

If u have some radio buttons in a radio group like this:

 

<input type="radio" name="radiosearch" value="City" />
<input type="radio" name="radiosearch" value="State" />
<input type="radio" name="radiosearch" value="Whatever" />

 

U can simply pass that values in your $SQL_WHERE variable:

 

<?php
$SQL_WHERE = $_POST['radiosearch'];
$getRecord_sql = "SELECT * FROM $SQL_FROM WHERE $SQL_WHERE LIKE '%$searchq%'";
?>

 

The same idea is for checkboxes too, if u want to search in multiple fields for the same keyword:

<input type="checkbox" name="checksearch[]" value="City" />
<input type="checkbox" name="checksearch[]" value="State" />
<input type="checkbox" name="checksearch[]" value="Whatever" />

 

Note the [] in the name of the checkbox, which makes those checkboxes a group and passes its values in an array:

<?php
$selected = $_POST['checksearch'];
$searchq = 'guilty'; //this will come from the input text
foreach($selected as $key=>$val){
$SQL_WHERE .= $val . " LIKE '%$searchq%'";
if($key < count($selected ) - 1){
	$SQL_WHERE  .= ' AND ';
}
}
$getRecord_sql = "SELECT * FROM $SQL_FROM WHERE $SQL_WHERE";
?>

 

Hope this helped.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.