What i'm doing is a optional search.. User has the choice to use one or more drop down menu for a search result.. What i did was to include <option selected value ''>Select One</option> , which is the default value for the drop down menu. The thing is whenever i submit, it searches the select one which i do not want it to..
How should i resolve this error that i have..
Address Bar:
http://localhost/searchAircraftTailNo.php?AircraftType=Select+One&ServicingType=AF&Submit=Search
<select name="AircraftType" id="AircraftType"><option selected value ''>Select One</option>
<?php while($rowAircraftType = mysql_fetch_array($resultAircraftType)) {
if($rowAircraftType['AircraftType']==$_GET['AircraftType'])
{
echo "<option selected value='$rowAircraftType[AircraftType]'>$rowAircraftType[AircraftType]</option>"."<BR>";}
else
{
echo "<option value='$rowAircraftType[AircraftType]'>$rowAircraftType[AircraftType]</option>";}
}
?>
</select>
The codes below should allow optional search
The Values would not be included if they are not empty (!empty($_GET['AircraftType'])
Hope i did it right..
<?php
if (isset($_GET['AircraftType']))
{
echo "Search results for: <b>" .$_GET['AircraftType']. " and " .$_GET['ServicingType']. "</b><br>";
//end highlight
$cntr = 1; //counter for the table columns
echo "<table width=90% align=center border=1><tr>
<td align=center bgcolor=#00FFFF>AirCraft Tail No.</td>
<td align=center bgcolor=#00FFFF>AirCraft Type</td>
<td align=center bgcolor=#00FFFF>Servicing Type</td>
<td align=center bgcolor=#00FFFF>Airframe Hours</td>
<td align=center bgcolor=#00FFFF>Defect Info</td>
<td align=center bgcolor=#00FFFF>Rectifications</td>
<td align=center bgcolor=#00FFFF>Comments</td>
<td align=center bgcolor=#00FFFF>Related Documents</td>
</tr><tr>";
$queryAircraftTypeDisplay = "SELECT * from report where 1";
if (!empty($_GET['AircraftType']))
{
$queryAircraftTypeDisplay .= " and aircraftType='".$_GET['AircraftType']."' ";
}
if (!empty($_GET['ServicingType']))
{
$queryAircraftTypeDisplay .= " and servicingType='".$_GET['ServicingType']."' ";
}
$resultAircraftTypeDisplay = mysql_query($queryAircraftTypeDisplay) or die ("couldn't execute query ".mysql_error());
while ($row = mysql_fetch_array($resultAircraftTypeDisplay))
{
//now let's make the keywords bold. To do that we will use preg_replace function.
//Replace field
$aircraftTailNo = $row[ '1' ];
$aircraftType = $row[ '2' ];
$servicingType = $row[ '3' ];
$airFrameHours = $row[ '4' ];
$defectInfo = $row[ '5' ];
$rectifications = $row[ '6' ];
$comments = $row[ '7' ];
$relatedDoc = $row[ '8' ];
//if there are 7 cols, write the <tr> tag and reset the counter
if ($cntr == 2)
{
echo "</tr><tr>";
$cntr =1;
}
echo "
<td>$aircraftTailNo </td>
<td>$aircraftType </td>
<td>$servicingType </td>
<td>$airFrameHours </td>
<td>$defectInfo </td>
<td>$rectifications </td>
<td>$comments </td>
<td>$relatedDoc </td>
";
$cntr++; //increment the counter
}//end while
echo "</tr></table>";
}
?>