...

View Full Version : allow either or both drop down to be selected and the other option remembered



FreelanceCoder
07-10-2012, 01:20 AM
I have two drop down menus and wish to allow the user to select from either or both and have them remember when the page reloads.

here is my code.


*************
EDIT: I have just added in the new part (just below the body tag) that I tried but still it is not remembering the previous option.
*************



<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<?
$newURL = "";
if ($_GET['F'] != "" || $_GET['S'] != "") { $newURL .= "?"; }
if ($_GET['F'] != "") { $newURL .= "F=" . $_GET['F']; }
if ($_GET['S'] != "") { $newURL .= "S=" . $_GET['S']; }
?>
<form action="<?=$_SERVER['PHP_SELF'] . $newURL;?>" method="GET">
<select name="F" onchange="this.form.submit();">
<option>Choose an option</option>
<option value="F1"<? if($_GET['F'] == "F1") echo " SELECTED"; ?>>F1</option>
<option value="F2"<? if($_GET['F'] == "F2") echo " SELECTED"; ?>>F2</option>
</select>
</form>

<br><br>
<form action="<?=$_SERVER['PHP_SELF'] . $newURL;?>" method="GET">
<select name="S" onchange="this.form.submit();">
<option value="S1"<? if($_GET['S'] == "S1" || $_GET['S'] != "S2") echo " SELECTED"; ?>>S1</option>
<option value="S2"<? if($_GET['S'] == "S2") echo " SELECTED"; ?>>S2</option>
</select>
</form>


</body>
</html>


so lets say they select (F)irst drop down menu and choose an option, the page would reload and the option they choose will be put back in that selection. Now if they were to now select the (S)econd drop down menu and choose an option along with the previously selected option in the First menu they have already done the page would reload and now both drop down menus would have their respective values that were chosen.

This should also be the case should they select from the (S)econd drop down to start with and then select from the (F)irst drop down afterwards the same thing should happen.

No matter which order they choose the options in.

It also need to keep the option they choose in the URL using GET as the method.

Currently my code does not allow this, it reloads the page but returns the options to their defaults apart the the most recently changed option.


Can anyone suggest what method I can use to store this so either or both are stored in the URL so my script can process the options. I have not added the rest of the code yet that would process as I have yet to got the options to stay selected. So will add this in later on.


*****
Oh and yes these MUST be two separate forms.

and no I do not wish to use jquery!
*****

FreelanceCoder
07-10-2012, 09:52 AM
i have made many changes after it was suggested that i use SESSIONS, but how do i get the URL to have both options that are selected.



<?
session_start();

echo("previous session data<br>");
print_r($_SESSION);
?>
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<?
if (isset($_GET['f'])) { $_SESSION['f'] = $_GET['f']; echo("<br>f was reset<br>"); }
if (isset($_GET['s'])) { $_SESSION['s'] = $_GET['s']; echo("<br>s was reset<br>"); }

echo("session now contains<br>");
print_r($_SESSION); ?>

<form action="<? echo($_SERVER['PHP_SELF'] . "?f=" . $_SESSION['f'] . "&amp;s=" . $_SESSION['s']); ?>" method="GET">
<select name="f" onchange="this.form.submit();">
<option>Choose an option</option>
<option value="F1"<? if($_SESSION['f'] == "F1") echo " SELECTED"; ?>>F1</option>
<option value="F2"<? if($_SESSION['f'] == "F2") echo " SELECTED"; ?>>F2</option>
</select>
</form>

<br><br>
<form action="<? echo($_SERVER['PHP_SELF'] . "?f=" . $_SESSION['f'] . "&amp;s=" . $_SESSION['s']); ?>" method="GET">
<select name="s" onchange="this.form.submit();">
<option value="S1"<? if($_SESSION['s'] == "S1" || $_SESSION['s'] != "S2") echo " SELECTED"; ?>>S1</option>
<option value="S2"<? if($_SESSION['s'] == "S2") echo " SELECTED"; ?>>S2</option>
</select>
</form>

</body>
</html>

sunfighter
07-10-2012, 03:44 PM
A suggestion, do not use the shortcut <? for starting php, most servers have gone to the latest version of php and the shortcut is disabled. <?= for <?php echo is also disabled.

Why are you having the drop down reload your page if you want the user to select from both of them??? In this case I'd use one form and a submit button.

P.S. To use sessions the session_start(); Must be your first line of code. You don't have it anywhere.


<?php
session_start();
plus more php

then your html

Arcticwarrio
07-10-2012, 05:26 PM
why cant you use just 1 form?

Arcticwarrio
07-10-2012, 05:32 PM
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<form action="" method="GET">
<select name="F" onchange="this.form.submit();">
<option>Choose an option</option>
<option value="F1"<? if($_GET['F'] == "F1"){echo " SELECTED";} ?>>F1</option>
<option value="F2"<? if($_GET['F'] == "F2"){echo " SELECTED";} ?>>F2</option>
</select>

<br><br>

<select name="S" onchange="this.form.submit();">
<option value="S1"<? if($_GET['S'] == "S1"){echo " SELECTED";} ?>>S1</option>
<option value="S2"<? if($_GET['S'] == "S2"){echo " SELECTED";} ?>>S2</option>
</select>
</form>

</body>
</html>

this does what you ask



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum