Page 1 of 1

Need help with echoing data from $_SESSION to <select>

Posted: Wed Sep 13, 2006 9:41 am
by christian_phpbeginner
Hello,

I have made a form in form.php where one of the form elements consist of <select> component to select the day of the month.

form.php:

Code: Select all

<?php
<form name="formTest" method="post" action = "blahblah.php">
   <select name = "day">
      <?php
         for ($i = 1; $i < 32; $i++) {
            echo "<option value = '$i'>".$i."</option>";           
         }
      ?>
   </select>
</form>
?>

Then, in the blahblah.php BEFORE THE HEADER I store the <option value> in $_SESSION['day'] variable, and also provided a form inside the <body> tag for the user to review their entry:
blahblah.php:

Code: Select all

<?php
$_SESSION['day'] = $_POST['day']; 
<form name="formTestReview" method="post" action="submit.php">
   <select name = "day">
      <?php
         for ($i = 1; $i < 32; $i++) {
            echo "<option value = '$i'>".$_SESSION['day']."</option>";           
         }
      ?>
   </select>
</form>
?>

Then, I am confused how to retrieve the selected day and display it in the formTestReview....If you test my code, you'll see that I am totally wrong....because of this here:

Code: Select all

echo "<option value = '$i'>".$_SESSION['day']."</option>";
I know by doing that, would cause the select options displays all the same selected date...but I don't know how to do the right one.

Please help me to retrieve the selected day in the form.php and display the value in the formTestReview within the <option> tag....thanks.

Chris

Posted: Wed Sep 13, 2006 9:59 am
by volka
You want to display another/new select with the previously chosen option selected again?
You have to mark this option element with the attribute selected, e.g.

Code: Select all

<option selected="selected">
Compare each the value of option element you print with the value stored in _SESSION. If the match, add selected="selected".

Halli hallo !

Posted: Wed Sep 13, 2006 10:30 am
by christian_phpbeginner
volka wrote:You want to display another/new select with the previously chosen option selected again?

You have to mark this option element with the attribute selected, e.g.

Code: Select all

<option selected="selected">
Compare each the value of option element you print with the value stored in _SESSION. If the match, add selected="selected".
Okay, how stupid I was. I just tried <option selected = "selected"> without matching them all, and the result was the "day" was selected by default to 31. So, ..I have to create another function to match the selected....lol...what a work.....

Danke !

Posted: Wed Sep 13, 2006 12:12 pm
by christian_phpbeginner
Does anybody have another solution ? Or the only way is what volka suggested ?

Thanks !

Posted: Wed Sep 13, 2006 2:15 pm
by feyd
That's the way it works. It's HTML.

Posted: Wed Sep 13, 2006 2:23 pm
by christian_phpbeginner
feyd wrote:That's the way it works. It's HTML.
I just made the function, and works fine. Thanks feyd. So, I guess this thread is SOLVED.

Chris

Posted: Wed Sep 13, 2006 2:53 pm
by RobertGonzalez
Just a quick thought...

Code: Select all

<?php
$_SESSION['day'] = $_POST['day'];
<form name="formTestReview" method="post" action="submit.php">
   <select name = "day">
      <?php
         for ($i = 1; $i < 32; $i++) {
            $selected = ($i == $_SESSION['day']) ? ' selected="selected"' : '';
            echo '<option value="' . $i . $selected . '">' . $i . '</option>';
         }
      ?>
   </select>
</form>
?>

Posted: Wed Sep 13, 2006 3:40 pm
by christian_phpbeginner
Everah wrote:Just a quick thought...

Code: Select all

<?php
$_SESSION['day'] = $_POST['day'];
<form name="formTestReview" method="post" action="submit.php">
   <select name = "day">
      <?php
         for ($i = 1; $i < 32; $i++) {
            $selected = ($i == $_SESSION['day']) ? ' selected="selected"' : '';
            echo '<option value="' . $i . $selected . '">' . $i . '</option>';
         }
      ?>
   </select>
</form>
?>
Hi Everah, I will try your code tomorrow to find out. Right now, I am too sleepy and need some rest...

Thank you

Chris