redisplay search criteria

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
marnieg
Forum Commoner
Posts: 65
Joined: Wed Mar 12, 2003 4:35 pm

redisplay search criteria

Post by marnieg »

I have a page that displays data from the database. The user has option of entering some search criteria and reset the page. This works, but I'm losing the search criteria after the data is displayed. Here is a portion of my code with the two forms.

Code: Select all

 <form name="search" method="post" action="<?=$PHP_SELF?>">
 <b>Seach for:</b> <input type="text" name="find" /> <b>in</b>
<Select NAME="field">
 <Option VALUE="job_nm">Job Name</option>
 <Option VALUE="job_num">Job Number</option>
 <Option VALUE="insp_nm">Inspector</option>
 <Option VALUE="wo_num">Work Order</option>
 <Option VALUE="wo_status">WO Status</option>
 </Select>
 <input type="hidden" name="searching" value="yes" />
 <input type="submit" name="search" value="Search" /> </form>
 <p>Work status is Active, Scheduled, Complete, or Cancelled</p>

		<?php
		include ("dbconnect.inc.php");
                if ($searching =="yes" and !$find == "") {
//Now we search for our search term, in the field the user specified
$query = mysql_query("Select * from work_orders,customer, jobs, inspector where wo_insp_id = insp_id and wo_cust_id =  cust_id and wo_job_id = job_id and upper($field) LIKE '%$find%' order by wo_num");
}
else
{
$query = mysql_query("Select * from work_orders,customer, jobs, inspector where wo_insp_id = insp_id and wo_cust_id =  cust_id and wo_job_id = job_id order by wo_num");
}
                    echo "<table border='1' style='border-collapse' width='95%' cellpadding='2'>
                    <td><b>WO Num<b></td>
                    <td><b>Customer<b></td>
                    <td><b>Job Name</b><b></td>
                    <td><b>Job Num<b></td>
                    <td><b>Inspector<b></td>
                    <td><b>WO Status<b></td>
                   <td><b>Edit<b></td>
                   <td><b>Delete<b></td>";
          while($row = mysql_fetch_array($query))
          {
          $inm = $row['insp_nm'];
          $wonum = $row['wo_num'];
          $custnm = $row['cust_nm'];
          $jobnm = $row['job_nm'];
          $jobnum = $row['job_num'];
          $wostat = $row['wo_status'];
          $woid = $row['wo_id'];
                    
                    //display results in a table
                    echo "<form name='results' method='post' action=' '>
                    <tr><td>$wonum</td>
                    <td>$custnm</td>
                    <td>$jobnm</td>
                    <td>$jobnum</td>
                    <td>$inm</td>
                    <td>$wostat</td>
                    <td><a href='workorder_upd.php?wo_id=$woid'>Edit</a></td>
                    <td><a href='workorder_del.php?wo_id=$woid'>Delete</a></td>
                    </tr>";
                    
          }
                    
          echo "</table></form><br></br>";
          $count = mysql_num_rows($query);
          if ($count == 0)
          {
          echo "Sorry, but we can not find an entry to match your query<br>"; exit;    
          }
          echo "<b>Search Criteria: </b> " .$find;
          mysql_close();
         ?>

So if a user chooses "Job Number" for the search criteria field and enter the job number I want the data to display AND keep the criteria.

Thanks for any assistance with this matter.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: redisplay search criteria

Post by Celauran »

Code: Select all

<Option VALUE="job_num" <?php echo ($_POST['field'] == "job_num") ? " selected=\"selected\"" : "">Job Number</option>
cpetercarter
Forum Contributor
Posts: 474
Joined: Sat Jul 25, 2009 2:00 am

Re: redisplay search criteria

Post by cpetercarter »

Use session variables or cookies to store the search criteria.
marnieg
Forum Commoner
Posts: 65
Joined: Wed Mar 12, 2003 4:35 pm

Re: redisplay search criteria

Post by marnieg »

I figured out that you need to conditionally echo the field based on what was posted. here is my form changes for future reference to others having this problem.

Code: Select all

 <form name="search" method="post" action="<?=$PHP_SELF?>">
 <b>Seach for:</b> <input type="text" name="find" value='<?php echo $find; ?>' /> <b>in</b>
<Select NAME="field">
 <Option VALUE="job_nm" <?php if($_POST['field'] == "job_nm"): ?>selected='selected'<?php endif;?>>Job Name</option>
 <Option VALUE="job_num" <?php if($_POST['field'] == "job_num"): ?>selected='selected'<?php endif;?>>Job Number</option>
 <Option VALUE="insp_nm" <?php if($_POST['field'] == "insp_nm"): ?>selected='selected'<?php endif;?>>Inspector</option>
 <Option VALUE="wo_num" <?php if($_POST['field'] == "wo_num"): ?>selected='selected'<?php endif;?>>Work Order</option>
 <Option VALUE="wo_status" <?php if($_POST['field'] == "wo_status"): ?>selected='selected'<?php endif;?>>WO Status</option>
 </Select>
 <input type="hidden" name="searching" value="yes" />
 <input type="submit" name="search" value="Search" /> </form>

Post Reply