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:


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

	$searchq		=	strip_tags($_GET['q']);
	$getRecord_sql	=	'SELECT * FROM '.$SQL_FROM.' WHERE '.$SQL_WHERE.' LIKE "'.$searchq.'%"';
	$getRecord		=	mysql_query($getRecord_sql);
	// ---------------------------------------------------------------- // 
	// 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!

Recommended Answers

All 2 Replies

I'm assuming that you have to employ an IF...ELSE statment, but I don't know how to utilize this with radio buttons...ugghh


Member Avatar for langsor

Played around with your code a little bit ...



  $SQL_FROM = 'adr';
  // for forms, use method="POST" unless good reason not to
  $radio = $_POST['radio_name']; 
  switch ( $radio ) {
    case 'first': $SQL_WHERE = 'first'; break;
    case 'city': $SQL_WHERE = 'city'; break;
    default: $SQL_WHERE = 'last';

  // or just use ... $SQL_WHERE =  $_POST['radio_name'];

  $searchq = strip_tags( $_POST['q'] );
  $getRecord_sql = 'SELECT * FROM '.$SQL_FROM.' WHERE '.$SQL_WHERE.' LIKE "'.$searchq.'%"';
  $getRecord = mysql_query($getRecord_sql);
  if( strlen($searchq) > 0 ){
  // ---------------------------------------------------------------- // 
  // AJAX Response                          // 
  // ---------------------------------------------------------------- // 
  print '     <ul>';
  while ( $list = mysql_fetch_object($getRecord) ) {
    $name = urlencode( $list->first.' '.$list->last );
    print <<<ENDLINE
        <li><a href="runners2.php?name=$name">$list->last <small>$list->first $list->city $list->age</small></a></li>
  print '     </ul>';

You could just use the value of the radio button as the field name, but then you're out a default field unless you have a radio button for 'last' value too, which could be checked in the html by default...otherwise use a switch statement to route your radio value ...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.