Page 1 of 1

How to remember the selected input?

Posted: Sun Sep 30, 2012 8:35 am
by Ghoz
Hi, I have a custom search for people page with option like age group, gender, country, etc. After the first search, the 'next' link will appear. The problem is when user click on this 'next' link no result is displayed because it cannot remember the entered/selected option. It will be back to the default option. Please help. I have been stuck here for many days! THANKS!!!

Code: Select all

<?php 
// This will allow to use the css inside includes if editing this file directly... probably
$visitingfiledirectly = false;
if($visitingfiledirectly)
{
	?>
    <link rel="stylesheet" type="text/css" media="screen" href="../stylus.css">
    <?php
}
?>
<?php
//To check default
$gender = "Female";
$age1 = "";
$age2 = "";
$country = "All";
$status = "";
$orderby = "BirthYear";

if(isset($_POST["gender"]) && ($_POST["gender"] == "Male" || $_POST["gender"] == "Female" || $_POST["gender"] == ""))
{
	$gender = $_POST["gender"];
}
if(isset($_POST["age1"]) && is_numeric($_POST["age1"]))
{
	$age1 = $_POST["age1"];
}
if(isset($_POST["age2"]) && is_numeric($_POST["age2"]))
{
	$age2= $_POST["age2"];
}
if(isset($_POST["country"]) && $_POST["country"] != "All")
{
	$country = PHP_slashes($_POST["country"]);
}
if(isset($_POST["status"]) && ($_POST["status"] == "Single" || $_POST["status"] == "Married"))
{
	$status = $_POST["status"];
}
if(isset($_POST["orderby"]) && ($_POST["orderby"] == "BirthYear" || $_POST["orderby"] == "Name"))
{
	$orderby = $_POST["orderby"];
}

$query = "";

//check if the starting row variable was passed in the URL or not
if (!isset($_GET['startrow']) or !is_numeric($_GET['startrow'])) {
$startrow = 0;
//otherwise we take the value from the URL
} else {
$startrow = (int)$_GET['startrow'];
}
$url = "index.php?section=browse";

$checkuser = "SELECT BirthYear, Id, Avatar, Name, Email FROM users WHERE %extend% LIMIT $startrow, 10"; // FIND THE DESIRED USER BY ID
//Name - to display the name
$extend = "";
$and = "";
if($gender != "")
{
	$extend = $extend.$and."Gender='".$gender."'";
	$and = " AND ";
}
	$extend = $extend.$and."BirthYear<='".(date("Y")-$age1)."'";
	$and = " AND ";
	$extend = $extend.$and."BirthYear>='".(date("Y")-$age2)."'";

if($country != "All")
{
	$extend = $extend.$and."Country='".$country."'";
	$and = " AND ";
}
if($status != "")
{
	$extend = $extend.$and."Status='".$status."'";
	$and = " AND ";
}
if($orderby == "BirthYear")
{
	$extend = $extend." ORDER BY BirthYear DESC";
}
if($orderby == "Name")
{
	$extend = $extend." ORDER BY Name ASC";
}
$checkuser = str_replace("%extend%", $extend, $checkuser);
$checkuser = mysql_query($checkuser);
//$user = mysql_fetch_array($checkuser); // COLLECT HIS DATA INTO AN ARRAY

$admin = getMyAdminLevel();
?>
<form method="POST" id="smform">
<b>Browse for People</b>
<font color="#FF0000"><br>You must signed up & log in to view the full profile.</font><br />
Gender: <input type="radio" name="gender" value="Male" <?php if($gender == 'Male')echo'checked';?> /> Male
<input type="radio" name="gender" value="Female" <?php if($gender == 'Female')echo'checked';?> /> Female
<input type="radio" name="gender" value="" <?php if(strlen($gender) == 0)echo'checked';?> /> Both<br>
Age:<input type="text" size="1" name="age1" <?php echo'value="'.$age1.'"'?>/>~<input type="text" size="1" name="age2" <?php echo'value="'.$age2.'"'?>/>
&#160;&#160;&#160;&#160;&#160;&#160;&#160;Location:<?php printCountries($country,TRUE);?>
<br />
Status:<select name="status">
<?php if($status != "")
{
echo'<option value="'.$status.'">'.$status.'</option>';
}?>
<option value="Single">Single</option>
<option value="Married">Married</option>
<option value="">Any</option>
</select>
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Order by:<input type="radio" name="orderby" value="BirthYear" <?php if($orderby == 'BirthYear')echo'checked';?> /> Age
<input type="radio" name="orderby" value="Name" <?php if($orderby == 'Name')echo'checked';?>/> Name &#160;&#160;&#160;&#160;&#160;&#160;
<input type="submit" value="Browse" id="sm">
</form> 
	<table border="0" align="center">
	<tr>
    <?php
	$counter = 0;
	while($user = mysql_fetch_array($checkuser))
	{
		?>
<td>
		<p align="center">
		<a target="_blank" href="index.php?section=profile&uid=<?php echo $user["Id"];?>"><?php echo $user["Name"];?></a>&#160;&#160;<br>
<!--["Name"] - to display the name-->
		<img src="<?php echo $user["Avatar"];?>" width="100" height="100"/></a></td></p>
        <?php
		$counter++;
		if($counter >= 5)
		{
			?></tr><tr><?php
			$counter = 0;
		}
	}
	?>

<?php
//Next page
?><div class="browse"><?php
$numrows = mysql_num_rows($checkuser);
if ($numrows >= 10) 
	echo '<a href="'.$url.'&startrow='.($startrow+10).'">Next</a>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;';
$prev = $startrow - 10;

//Previous page
if ($prev >= 0)
    echo '<a href="'.$url.'&startrow='.$prev.'">Previous</a>';
?>

</table>
</div>

Re: How to remember the selected input?

Posted: Sun Sep 30, 2012 9:20 am
by McInfo
1. You could use the GET method on your form; then propagate the search terms through the URL. That's what Google's search page does.

2. You could turn your pagination links into form buttons and resubmit the search terms through hidden form inputs. That's not a great idea, though, because applications should return a Location header with a 303 status code after a POST so users can use their Back button without getting a "document expired" or "do you want to resubmit this form" message from their browser.

3. You could store either the search terms or the results in session variables.

Re: How to remember the selected input?

Posted: Sun Sep 30, 2012 8:00 pm
by Ghoz
Hi, basically I understand what you are saying but when it comes to the coding it's not that simple to me. I'm a newbie.. Any clear guide anyone?? Thanks

Re: How to remember the selected input?

Posted: Mon Oct 01, 2012 4:02 am
by Grizzzzzzzzzz
If you're looking for a guide for this kind of stuff, try looking through these 2 as they cover the very basics of what you're trying to accomplish

http://www.w3schools.com/php/php_get.asp
http://www.w3schools.com/php/php_sessions.asp