Forum Moderators: coopster
On a page i have a form which allows a user to enter a keyword, and selecting an option in three
"<select><option value=" "> </option></select>" fields
and then click search.
For exemple :
first page :
<form name= formS method="post" action="jobSearchTreatment.php" >
<label style="font-weight:bold;">keywords</label>: <br />
<input id="keywords" type="text" name="keywords" value="" size="24" maxlength="100" />
<label style="font-weight:bold;">category</label>:
<select name="category" id ="category" size="1" style="width:300px;">
<option selected="selected" value="ALL">All-----</option>
<option value="Accounting/Finance">Accounting/Finance</option>
<option value="Advertising/PR">Advertising/PR</option>
<option value="Banking/Mortgage">Banking/Mortgage</option>
<option value="College">College</option>
</select>
<label style="font-weight:bold;">Location</label>:
<select name="location" id ="location" size="1" style="width:300px;">
<option selected="selected" value="ALL">All-----</option>
<option value="CANADA">CANADA</option>
<option value="Alberta">Alberta</option>
<option value="British Columbia">British Columbia</option>
<option value="Manitoba">Manitoba</option>
</select>
<label style="font-weight:bold;">City</label>:
<select name="city" id ="city" size="1" style="width:300px;">
<option selected="selected" value="ALL">All-----</option>
<option value="Montreal">Montreal</option>
<option value="Quebec">Quebec</option>
<option value="Drummondville">Drummondville</option>
<option value="Granby">Granby</option>
</select>
<input type="submit" name="search" value="search" id="Search" />
</form>
-On the second file (jobSearchTreatment.php) I have :
<?php
session_start();
error_reporting(E_ALL);
$keywordsP = @$_POST['keywords'];
$_SESSION['cat'] = @$_POST['category'];
$_SESSION['city'] = @$_POST['city'];
$_SESSION['loc'] = @$_POST['location'];
$categoryP = $_SESSION['cat'];
$cityP = $_SESSION['city'];
$locationP = $_SESSION['loc'];
$trimmed = trim($keywordsP);
$srch = "%".$trimmed."%";
//connexion
$host ='--------';
$user ='------';
$pasword ='-----';
$connexion = mysql_connect($host,$user,$pasword) or die ("impossible connexion $host");
mysql_select_db("d60409939",$connexion)or die("cannot select DB" .mysql_error());
// Number of records to show per page
$recordsPerPage = 25; # 0
// default startup page
$pageNum = 1;
if(isset($_GET['p'])) { $pageNum = $_GET['p']; settype($pageNum, 'integer');}
$offset = ($pageNum - 1) * $recordsPerPage;
if (($_SESSION['cat'] == "ALL") && ($_SESSION['loc'] == "ALL") && ($_SESSION['city'] == "ALL"))
{
$query = "SELECT * FROM EmployerJobsPostingDB WHERE jobTitle LIKE '$srch' ORDER BY postingDate DESC LIMIT $offset, $recordsPerPage;";
}
$result = mysql_query($query) or die('Mysql Err. 1');
while($row = mysql_fetch_assoc($result)){
echo'<td class ="leftcol"><font color="000000">'.$row['column'].'</font></td>';
}
$query = "SELECT COUNT(jobTitle) AS dt FROM EmployerJobsPostingDB WHERE jobTitle LIKE '$srch';"; # 3
$result = mysql_query($query) or die('Mysql Err. 2');
$row = mysql_fetch_assoc($result);
$numrows = $row['dt']; # 4
$maxPage = ceil($numrows/$recordsPerPage);
$nav = '';
for($page = 1; $page <= $maxPage; $page++)
{
if ($page == $pageNum)
{
$nav .= "<div class=\"pNo\">$page</div>";
}
else
{
$nav .= "<div class=\"pNo\"><a href=\"?p=$page\">$page</a></div>";
}
}
if ($pageNum > 1)
{
$page = $pageNum - 1;
$prev = "<a href=\"?p=$page &category=$categoryP &city=$cityP &location=$locationP \"><strong>Previous</strong></a>";
$first = "<a href=\"?p=1\"><strong><<</strong></a>";
}
else
{
$prev = '<strong> </strong>';
$first = '<strong> </strong>';
}
if ($pageNum < $maxPage)
{
$page = $pageNum + 1;
$next = "<a href=\"?p=$page &category=$categoryP &city=$cityP &location=$locationP \"><strong>Next</strong></a>";
$last = "<a href=\"?p=$maxPage\"><strong>>></strong></a>";
}
else
{
$next = '<strong> </strong>';
$last = '<strong> </strong>';
}
if ($maxPage > 1)
{
$datas = '<table class="data" border="0" cellpadding="3" cellspacing="0" rules="rows" frame="below">';
echo'<center>';
echo $datas;
echo '<tr>';
echo '<td>';
echo "<div class=\"pagingDiv\">
<div class=\"pNo\">$first</div>
<div class=\"pNo\">$prev</div>
$nav
<div class=\"pNo\">$next</div>
<div class=\"pNo\">$last</div></div>";
echo '</td>';
echo '</tr>';
echo '</table>';
echo'</center>';
}
?>
end of the second page.
my problem is that, i want that if a user enter a keyword and select 1 or more option in the select field in the first page. It will work for the first page. But if i have more than 1 page to visualize results, when i click the second page it does not work. it gives me this error message :
"Notice: Undefined variable: query in ......"
The variables from the "select option" field lost their values.
If you can give me sugestion that will be nice.
Thank u for replying,I know that i can use session, but as u can see on the code that i posted in my first message, i'm using session.
I created 3 variables session, each session's variable is equal to each <select><option> field selected by the user. but it's still not working. May be i'm not doing in the right way.
But the second option u told me, is to create hidden field in the form, can u give a good exemple please.
Thank you
if ($_POST['submit']) {
$keywordsP = @$_POST['keywords'];
$_SESSION['cat'] = @$_POST['category'];
$_SESSION['city'] = @$_POST['city'];
$_SESSION['loc'] = @$_POST['location'];
}
> Shouldn't the keywords be stored in the session variable too?
if ($_POST['submit']) {
$_SESSION['keywords'] = @$_POST['keywords'];
$_SESSION['cat'] = @$_POST['category'];
$_SESSION['city'] = @$_POST['city'];
$_SESSION['loc'] = @$_POST['location'];
}
$keywordsP = $_SESSION['keywords'];
. . .
> A few recommendations, not related directly to the problem but good if you implement it:
* Clean up the input you get from the form (security)
* Connection to the mysql database should be stored in a separate file, especially if you intend to include it in many pages, as you will have a much more cleaner page.
* You have error_reporting(E_ALL) and silenced (@) the errors which might be there on the codes. (You should probably not report all errors when your page goes live)
. . . and Welcome to WebmasterWorld.
Habtom