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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
christian_phpbeginner
Forum Contributor
Posts: 136
Joined: Sat Jun 03, 2006 2:43 pm
Location: Java

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

Post 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
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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".
User avatar
christian_phpbeginner
Forum Contributor
Posts: 136
Joined: Sat Jun 03, 2006 2:43 pm
Location: Java

Halli hallo !

Post 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 !
User avatar
christian_phpbeginner
Forum Contributor
Posts: 136
Joined: Sat Jun 03, 2006 2:43 pm
Location: Java

Post by christian_phpbeginner »

Does anybody have another solution ? Or the only way is what volka suggested ?

Thanks !
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

That's the way it works. It's HTML.
User avatar
christian_phpbeginner
Forum Contributor
Posts: 136
Joined: Sat Jun 03, 2006 2:43 pm
Location: Java

Post 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
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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>
?>
User avatar
christian_phpbeginner
Forum Contributor
Posts: 136
Joined: Sat Jun 03, 2006 2:43 pm
Location: Java

Post 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
Post Reply