Search form issue

Hi I currently have a page with a search box with several check-boxes that allow people to specify their search. It works perfectly however it is all on one page. I want to have the search box on another page and when the user presses search it display the results on another page. How do I go about doing this?

I’ve tried changing the form action to the new page however it does not display any results.

The is the current code:

(top of the page)
[php]

<?php $dbHost = 'localhost'; // set these to your mysql database username and password. $dbUser = '#####'; $dbPass = #####'; $dbDatabase = '######'; // the database you put the table into. $con = mysql_connect($dbHost, $dbUser, $dbPass) or trigger_error("Failed to connect to MySQL Server. Error: " . mysql_error()); mysql_select_db($dbDatabase) or trigger_error("Failed to connect to database {$dbDatabase}. Error: " . mysql_error()); // Set up our error check and result check array $error = array(); $results = array(); // First check if a form was submitted. // Since this is a search we will use $_GET if (isset($_GET['search'])) { $searchTerms = trim($_GET['search']); $searchTerms = strip_tags($searchTerms); if (strlen($searchTerms) < 3) { $error[] = "Search terms must be longer than 3 characters."; }else { $searchTermDB = mysql_real_escape_string($searchTerms); } // If there are no errors, lets get the search going. if (count($error) < 1) { $searchSQL = "SELECT ID, FirstName, LastName, Email, PhoneNumber, MobileNumber, Department, Location, HoursWorked FROM users WHERE "; // grab the search types. $types = array(); $types[] = isset($_GET['ID'])?"`ID` LIKE '%{$searchTermDB}%'":''; $types[] = isset($_GET['FirstName'])?"`FirstName` LIKE '%{$searchTermDB}%'":''; $types[] = isset($_GET['LastName'])?"`LastName` LIKE '%{$searchTermDB}%'":''; $types[] = isset($_GET['Email'])?"`Email` LIKE '%{$searchTermDB}%'":''; $types[] = isset($_GET['PhoneNumber'])?"`PhoneNumber` LIKE '%{$searchTermDB}%'":''; $types[] = isset($_GET['MobileNumber'])?"`MobileNumber` LIKE '%{$searchTermDB}%'":''; $types[] = isset($_GET['Department'])?"`Department` LIKE '%{$searchTermDB}%'":''; $types[] = isset($_GET['Location'])?"`Location` LIKE '%{$searchTermDB}%'":''; $types[] = isset($_GET['HoursWorked'])?"`HoursWorked` LIKE '%{$searchTermDB}%'":''; $types = array_filter($types, "removeEmpty"); // removes any item that was empty (not checked) if (count($types) < 1) $types[] = "`FirstName` LIKE '%{$searchTermDB}%'"; // use the body as a default search if none are checked $andOr = isset($_GET['matchall'])?'AND':'OR'; $searchSQL .= implode(" {$andOr} ", $types) . " ORDER BY `FirstName`"; // order by title. $searchResult = mysql_query($searchSQL) or trigger_error("There was an error.
" . mysql_error() . "
SQL Was: {$searchSQL}"); if (mysql_num_rows($searchResult) < 1) { $error[] = "The search term provided {$searchTerms} yielded no results."; }else { $results = array(); // the result array $i = 1; while ($row = mysql_fetch_assoc($searchResult)) { $results[] = " ID: {$row['ID']}
Name: {$row['FirstName']}{$row['LastName']}
Email Address: {$row['Email']}
Phone Number: {$row['PhoneNumber']}
Mobile Number: {$row['MobileNumber']}
Department: {$row['Department']}
Location:{$row['Location']}
Hours Worked: {$row['HoursWorked']}

"; $i++; } } } } function removeEmpty($var) { return (!empty($var)); } ?>

[/php]

The form
[php]

<?php echo (count($error) > 0)?"The following had errors:
" . implode("
", $error) . "


":""; ?>
  <form method="GET" action="<?php echo $_SERVER['PHP_SELF'];?>" name="searchForm">
     Search For: <input type="text" name="search" value="<?php echo isset($searchTerms)?htmlspecialchars($searchTerms):''; ?>" /> 
              <br />
     Search In:<br />
     First Name: <input type="checkbox" name="FirstName" value="on" <?php echo isset($_GET['FirstName'])?"checked":''; ?> />  
     Last Name: <input type="checkbox" name="LastName" value="on" <?php echo isset($_GET['LastName'])?"checked":''; ?> />  
     Email: <input type="checkbox" name="Email" value="on" <?php echo isset($_GET['Email'])?"checked":''; ?> /> 
      ID: <input type="checkbox" name="ID" value="on" <?php echo isset($_GET['ID'])?"checked":''; ?> />
      Location: <input type="checkbox" name="Location" value="on" <?php echo isset($_GET['ID'])?"checked":''; ?> />
      <br />
             <br />
     <input type="submit" name="submit" value="Search!" />
  </form>
  <?php echo (count($results) > 0)?"Your search term: {$searchTerms} returned:<br /><br />" . implode("", $results):""; ?>

[/php]

code for test.php

[php]<?php

// Set up our error check and result check array
$error = array();

// First check if a form was submitted.
// Since this is a search we will use $_GET
if (isset($_GET[‘search’])) {
$searchTerms = trim($_GET[‘search’]);
$searchTerms = strip_tags($searchTerms);

if (strlen($searchTerms) < 3) {
$error[] = “Search terms must be longer than 3 characters.”;
}else {
$searchTermDB = mysql_real_escape_string($searchTerms);
}

// If there are no errors, lets get the search going.
if (count($error) < 1) {
header(“Location:result.php?search=$searchTermDB”);
}
}

function removeEmpty($var) {
return (!empty($var));
}
?>

<?php echo (count($error) > 0)?"The following had errors:
" . implode("
", $error) . "


":""; ?>
  <form method="GET" action="<?php echo $_SERVER['PHP_SELF'];?>" name="searchForm">
     Search For: <input type="text" name="search" value="<?php echo isset($searchTerms)?htmlspecialchars($searchTerms):''; ?>" /> 
              <br />
     Search In:<br />
     First Name: <input type="checkbox" name="FirstName" value="on" <?php echo isset($_GET['FirstName'])?"checked":''; ?> />  
     Last Name: <input type="checkbox" name="LastName" value="on" <?php echo isset($_GET['LastName'])?"checked":''; ?> />  
     Email: <input type="checkbox" name="Email" value="on" <?php echo isset($_GET['Email'])?"checked":''; ?> /> 
      ID: <input type="checkbox" name="ID" value="on" <?php echo isset($_GET['ID'])?"checked":''; ?> />
      Location: <input type="checkbox" name="Location" value="on" <?php echo isset($_GET['ID'])?"checked":''; ?> />
      <br />
             <br />
     <input type="submit" name="submit" value="Search!" />
  </form>

[/php]

code for result.php

[php]<?php

$searchTermDB = $_GET[‘search’];

/echo “

”;
print_r($_REQUEST);
/

$results = array();

//echo $searchTermDB;

$dbHost = ‘localhost’;
// set these to your mysql database username and password.
$dbUser = ‘#####’;
$dbPass = ‘#####’;
$dbDatabase = ‘######’; // the database you put the table into.
$con = mysql_connect($dbHost, $dbUser, $dbPass) or die("Failed to connect to MySQL Server. Error: " . mysql_error());

mysql_select_db($dbDatabase) or die("Failed to connect to database {$dbDatabase}. Error: " . mysql_error());

$searchSQL = "SELECT ID, FirstName, LastName, Email, PhoneNumber, MobileNumber, Department, Location, HoursWorked FROM users WHERE ";

  // grab the search types.
  $types = array();

$types[] = isset($_GET[‘ID’])?"ID LIKE ‘%{$searchTermDB}%’":’’;
$types[] = isset($_GET[‘FirstName’])?"FirstName LIKE ‘%{$searchTermDB}%’":’’;
$types[] = isset($_GET[‘LastName’])?"LastName LIKE ‘%{$searchTermDB}%’":’’;
$types[] = isset($_GET[‘Email’])?"Email LIKE ‘%{$searchTermDB}%’":’’;
$types[] = isset($_GET[‘PhoneNumber’])?"PhoneNumber LIKE ‘%{$searchTermDB}%’":’’;
$types[] = isset($_GET[‘MobileNumber’])?"MobileNumber LIKE ‘%{$searchTermDB}%’":’’;
$types[] = isset($_GET[‘Department’])?"Department LIKE ‘%{$searchTermDB}%’":’’;
$types[] = isset($_GET[‘Location’])?"Location LIKE ‘%{$searchTermDB}%’":’’;
$types[] = isset($_GET[‘HoursWorked’])?"HoursWorked LIKE ‘%{$searchTermDB}%’":’’;

  $types = array_filter($types, "removeEmpty"); // removes any item that was empty (not checked)
  
  if (count($types) < 1)
     $types[] = "`FirstName` LIKE '%{$searchTermDB}%'"; // use the body as a default search if none are checked
  
      $andOr = isset($_GET['matchall'])?'AND':'OR';
  $searchSQL .= implode(" {$andOr} ", $types) . " ORDER BY `FirstName`"; // order by title.

  $searchResult = mysql_query($searchSQL) or trigger_error("There was an error.<br/>" . mysql_error() . "<br />SQL Was: {$searchSQL}");
  
  if (mysql_num_rows($searchResult) < 1) {
     $error[] = "The search term provided {$searchTerms} yielded no results.";
  }else {
     $results = array(); // the result array
     $i = 1;
     while ($row = mysql_fetch_assoc($searchResult)) {
        $results[] = " ID:&nbsp;{$row['ID']}<br />Name:&nbsp;{$row['FirstName']}{$row['LastName']}<br />Email Address:&nbsp;{$row['Email']}<br />Phone Number:&nbsp;{$row['PhoneNumber']}<br />Mobile Number:&nbsp;{$row['MobileNumber']}<br />Department:&nbsp;{$row['Department']}<br />Location:{$row['Location']}<br />Hours Worked:&nbsp;{$row['HoursWorked']}<br /><br />";
        $i++;
     }
  }

?>
<?php echo (count($results) > 0)?"Your search term: {$searchTerms} returned:

" . implode("", $results):""; ?>

[/php]

I’m getting an error saying ‘cannot modify header information’

A very common error! It occurs when you use echo to display some text before the header() function.

It would be better if you go through this : http://digitalpbk.com/php/warning-cannot-modify-header-information-headers-already-sent

Also, http://www.satya-weblog.com/2007/05/warning-cannot-modify-header.html

Sponsor our Newsletter | Privacy Policy | Terms of Service