Jump to content

Retaining selected values


phatgreenbuds

Recommended Posts

This was originally posted in the javascript forum and it was suggested this is more of a PHP issue so here I am.

 

I am just starting down this path of javascript, seeing where I can enhance a php/mysql page with it. One thing I see right off is the ability to have pages appear to update dynamically without the need to use a submit button.  My plan is to try and create a branching form that depending on what options are selected in one section a resultant set of options will appear accordingly in subsequent sections.

 

I have been playing with this little bit of code that I found and have a few questions...

 

<form action="./tester.php" method="GET">
<div align="center"">
  <select name="state" onchange="this.form.submit();">
    <option>Choose One To Submit This Form</option>
    <option value="CA">CA</option>
    <option value="TX">TX</option>
  </select>
</div>
</form>

<?php 

$test = $_GET['state'];
//echo $test;

if ($test == "CA") {
   echo "Ughhh another Liberal";
//this is the first branch where one set of options will appear
} else {
   if ($test == "TX") {
      echo "You Selected Texas...You ROCK!";
//this is the alternate branch where another set of options will appear
      }
   }
?>

 

This works so far and I admit I do not fully understand it yet but the fog is lifting.  I'm curious how would I prevent the dropdown from resetting and instead change it to reflect the selected option? And as I start to branch this form further out for more options what would be the best way to preserve the previous selections. I know that somehow I have to change the <option> tag to show "selected" just not sure how to do so.

Link to comment
Share on other sites

<form action="./tester.php" method="GET">
<div align="center">
  <select name="state" onchange="this.form.submit();">
    <option>Choose One To Submit This Form</option>
    <option value="CA"<?PHP echo ($_GET['state'] == "CA")?" selected=\"selected\"":NULL; ?>>CA</option>
    <option value="TX"<?PHP echo ($_GET['state'] == "TX")?" selected=\"selected\"":NULL; ?>>TX</option>
  </select>
</div>
</form>

 

On a side note, there's usually no such thing as an "align" property. Try...

 

<div style="margin-left: auto; margin-right: auto;">CONTENT</div>

Link to comment
Share on other sites

ahhhhh so if I were to take it one step further I could change

 

<option value="CA"<?PHP echo ($_GET['state'] == "CA")?" selected=\"selected\"":NULL; ?>>CA</option>

 

to something like this and simplify the whole thing for say 100 different options

 

<option value="CA"<?PHP echo ($_GET['state'] == $_POST['state'])?" selected=\"selected\"":NULL; ?>>CA</option>

 

Or am I still not getting it? Gonna try and see what happens.  BTW thanks for the "align" correction...

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.