Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Jan 2012
    Posts
    32
    Thanks
    0
    Thanked 1 Time in 1 Post

    allow either or both drop down to be selected and the other option remembered

    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.
    *************

    Code:
    <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!
    *****
    Last edited by FreelanceCoder; 07-10-2012 at 12:37 AM.

  • #2
    New Coder
    Join Date
    Jan 2012
    Posts
    32
    Thanks
    0
    Thanked 1 Time in 1 Post
    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.

    Code:
    <?
    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>

  • #3
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,226
    Thanks
    23
    Thanked 606 Times in 605 Posts
    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.
    Code:
    <?php
    session_start();
    plus more php
    
    then your html
    Last edited by sunfighter; 07-10-2012 at 02:53 PM.

  • #4
    Regular Coder Arcticwarrio's Avatar
    Join Date
    May 2012
    Location
    UK
    Posts
    721
    Thanks
    20
    Thanked 84 Times in 84 Posts
    why cant you use just 1 form?

  • #5
    Regular Coder Arcticwarrio's Avatar
    Join Date
    May 2012
    Location
    UK
    Posts
    721
    Thanks
    20
    Thanked 84 Times in 84 Posts
    PHP Code:
    <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


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •