PRodgers4284 Posted March 9, 2008 Share Posted March 9, 2008 I have a basic search facility using two drop down options, the search works fine but when i open the search script it displays all the records in the database, i dont want any records display until the submit button is selected. Can anyone help me with this: My code is: <!--Search Job form --> Job Search:<form method="post" action=""> Job Category: <select name="jobcatergory"> <option value="Please Select">Please Select</option> <option value="Construction">Construction</option> <option value="Banking and Insurance">Banking and Insurance</option> <option value="Sales">Sales</option> <option value="Other">Other</option> </select> Job Location: <select name="joblocation"> <option value="Please Select">Please Select</option> <option value="Co.Antrim">Co.Antrim</option> <option value="Co.Down">Co.Down</option> </select> <input type="submit" value="Find a Job" /> </form> <?php $sql = "SELECT * FROM job"; $jobcat = mysql_real_escape_string(trim($_POST['jobcatergory'])); $jobloc = mysql_real_escape_string(trim($_POST['joblocation'])); if ($jobcat != '' && $jobloc != '') { $sql .= " WHERE jobcatergory LIKE '$jobcat%' AND joblocation LIKE '%$jobloc%'"; } else if ($jobcat != '' && $jobloc == '') { $sql .= " WHERE jobcatergory LIKE '%$jobcat%'"; } else if ($jobloc != '' && $jobcat == '') { $sql .= " WHERE joblocation LIKE '%$jobloc%'"; } $query = mysql_query($sql) or die(mysql_error()); if(mysql_num_rows($query) > 0) { while ($job = mysql_fetch_array($query)) { $jobtitle=$job["jobtitle"]; $jobcatergory=$job["jobcatergory"]; ?> <table border="1" width="51%" id="table1" bgcolor="#FFFFFF"> <tr> <td width="131"><font face="Verdana" size="2">Job Title</font></td> <td width="131"><font face="Verdana" size="2">Job Catergory</font></td> <td width="131"><font face="Verdana" size="2">Job Description</font></td> </tr> <tr> <td width="131"><font face="Verdana" size="2"><?php echo $job["jobtitle"]; ?></font></td> <td width="148"><font face="Verdana" size="2"><?php echo $job["jobcatergory"]; ?></font></td> <td width="148"><font face="Verdana" size="2"><?php echo $job["description"]; ?></font></td> </tr> </table> <?php } } else { echo '<p>There are no search results with the search criteria you entered.</p>'; } ?> Quote Link to comment Share on other sites More sharing options...
BlueSkyIS Posted March 9, 2008 Share Posted March 9, 2008 wrap the whole search code in an IF to check to see if the form was posted if ($_SERVER['REQUEST_METHOD'] == "post") { // put all search code in here. } Quote Link to comment Share on other sites More sharing options...
PRodgers4284 Posted March 9, 2008 Author Share Posted March 9, 2008 wrap the whole search code in an IF to check to see if the form was posted if ($_SERVER['REQUEST_METHOD'] == "post") { // put all search code in here. } Hey thanks for the reply, i tried what u suggested but the search stopped working. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.