I am creating a simple search feature that queries a user input string for a matches in a products catalog that is held within a table in mysql.
This is my code for this search form:
<form name="search" method="post" action="'.$_SERVER['PHP_SELF'].'">
<div>
Search For Product: <input type="text" name="find" style="background: #F4F4F4; font-family: "Lucida Console", Monaco, monospace;" />
<input type="hidden" name="searching" value="yes" />
<input type="submit" name="search" value="Search" />
</div>
</form>
Here is the code that processes this string:
//This is only displayed if they have submitted the form
if ($searching == "yes")
{
$pageContent = '
<h2>Place your order</h2>
<p>Your order contains '.count($_SESSION['order']).' items. (<a href="?orders">View your order</a>)</p>
<form name="search" method="post" action="'.$_SERVER['PHP_SELF'].'">
<div>
Search For Product: <input type="text" name="find" style="background: #F4F4F4; font-family: "Lucida Console", Monaco, monospace;" />
<input type="hidden" name="searching" value="yes" />
<input type="submit" name="search" value="Search" />
</div>
</form>
<br />
';
//If they did not enter a search term we give them an error
if ($find == "")
{
$pageContent .= '<p>You forgot to enter a search term</p>';
echo $head1 . $pageDetails . $head2 . $header . $menu . $belowMenu . $content . $pageContent . $footer . $pageScripts;
exit;
}
// Otherwise we connect to our Database
$bccConn = mysql_connect($bccHost, $bccUser, $bccPass) or exit(mysql_error());
mysql_select_db($bccDB, $bccConn) or exit(mysql_error());
// We preform a bit of filtering
$find = strtoupper($find);
$find = strip_tags($find);
$find = trim ($find);
$keywords_array = explode(' ', $find);
//Now we search for our search term, in the field the user specified
$dataQuery = "SELECT * FROM `products` WHERE upper(`desc`) LIKE '%"
. implode("%' AND upper(`desc`) LIKE '%", $keywords_array)
. "%'";
$data = mysql_query($dataQuery) or die(mysql_error());
//And we remind them what they searched for
$pageContent .= '
<p><b>Searched For:</b> ' . $find . '</p>
';
$tempVar = 0;
//And we display the results
while ($result = mysql_fetch_array($data)) {
$prId = $result['id'];
$prRefCode = $result['refCode'];
$prDesc = $result['desc'];
$prPack = $result['pack'];
$prMeasure = $result['measure'];
$prQuantity = $result['quantity'];
$prDeptCode = $result['deptCode'];
$prTaxable = $result['taxable'];
$prPrice1 = $result['price1'];
$prPrice2 = $result['price2'];
$prCrdCode = $result['crdCode'];
$prCost1 = $result['cost1'];
$prCost2 = $result['cost2'];
if ($tempVar == 0) {
$pageContent .= '
<p>All prices are inclusive of VAT</p>
<table border="1">
<thead>
<tr>
<th>Stock Code</th>
<th>Description</th>
<th>Packsize</th>
<th>Price</th>
<th>In-Stock?</th>
<th>Quantity</th>
<th>Submit</th>
</tr>
</thead>
<tbody>
';
}
if ($tempVar == mysql_num_rows($data)) {
$pageContent .= '
</tbody>
</table>
';
}
$pageContent .= '
<tr>
<td>' . $prId . '</td>
<td>' . $prDesc . '</td>
<td>' . $prPack . 'x' . $prSize . ' ' . $prMeasure . '</td>
<td>R' . $prPrice1 . '</td>
';
if (empty($prQuantity)) {
$pageContent .= '
<td>No</td>
';
} else {
$pageContent .= '
<td>Yes</td>
';
}
$pageContent .= '
<form action="" method="post">
<td>
<div>
<input type="text" name="quantity" size ="2" value ="1" style="background: #F4F4F4; font-family: "Lucida Console", Monaco, monospace;"/>
</div>
</td>
<td>
<div>
<input type="hidden" name="id" value="' . $prId . '" />
<input type="submit" name="action" value="Order" />
</div>
</td>
</form>
</tr>
';
$tempVar ++;
}
$pageContent .= '
</tbody>
</table>
';
//This counts the number or results - and if there wasn't any it gives them a little message explaining that
$anymatches = mysql_num_rows($data);
if ($anymatches == 0) {
$pageContent .= '
<p>Sorry, but we can not find an entry to match your query</p>
';
}
}
Here is the code that deals with the event of a user clicking the "Order" button:
if (!isset($_SESSION['order']))
{
$_SESSION['order'] = array();
}
if (!isset($_SESSION['quantity']))
{
$_SESSION['quantity'] = array();
}
if (isset($_POST['action']) and $_POST['action'] == 'Order')
{
// Add item to the end of the $_SESSION['order'] array
$_SESSION['order'][$_POST['id']] = $_POST['id'];
$_SESSION['quantity'][$_POST['id']] = $_POST['quantity'];
header('Location: .');
exit();
}
What I would like to happen, is when the user clicks on order, after the header redirect, the page should not be directed to the folder index, instead, it should be refreshed with the current search queries results intact.
At the moment, when the user clicks on order, they are redirected to the index page, and in this process, lose whatever search results they had previously called.
I understand that I would need to add the users search string to a session, then recall this information after the page has been refreshed.
What I am struggling with though, is the process involved in this.
If anyone has any ideas on how i would go about this, I would really appreciate the advice!
Thanks!