setting a value in html select option using php

Discussing Html . Code , Software , other

Moderators: macek, egami, gesf

JohnPringle83
New php-forum User
New php-forum User
Posts: 8
Joined: Tue May 03, 2011 5:56 am

setting a value in html select option using php

Postby JohnPringle83 » Tue May 03, 2011 6:09 am

Hello I am creating a user registration form and I have a section where they state their date of birth. I am trying to make it so that if there is an error somewhere in the form that the values in the select option do not reset to their default.

This is the code I have tried, I hope you can see what it is that I am trying to acheive. I have left out unessecary parts of the code such as table structure and the entire options list.

Code: Select all

<select name="day" value="<?php if($_POST['day']!="00"){echo $_POST['day'];}?>">
   <option value ="00">Day</option>
   <option value ="01">1</option>
   <option value ="02">2</option>
   <option value ="03">3</option>
</select>

<select name="month" value="<?php if($_POST['month']!="00"){echo $_POST['month'];}?>">
   <option value ="00">Month</option>
   <option value ="01">January</option>
   <option value ="02">February</option>
</select>

<select name="year" value="<?php if($_POST['year']!="00"){echo $_POST['year'];}?>">
   <option value ="00">Year</option>
   <option value ="1950">1950</option>
   <option value ="1951">1951</option>
   <option value ="1952">1952</option>
</select>

User avatar
Nullsig
php-forum Fan User
php-forum Fan User
Posts: 981
Joined: Thu Feb 17, 2011 6:52 am
Location: Racine, WI

Re: setting a value in html select option using php

Postby Nullsig » Tue May 03, 2011 7:43 am

Since you left out the necessary "unnecessary" parts of the code I.E.

Form Tags - so I can see if this page is posting to itself or to another page.

The part where the form is processed - So I can see how you are detecting errors and posting back to the form.



I can't really tell you for certain how to make this form "sticky" but if you add the validation to the page via JavaScript then it will detect the error on submit and never refresh the page, which means all the values will stay there.

There is a post I made about just such a form validation script here:
http://www.php-forum.com/phpforum/viewtopic.php?f=2&t=12014&p=4376387&hilit=validate_form#p4376387


If on the other hand you want to handle this in purely PHP, essentially what you have to do is send the variables back to the form and use their values. In the case of maintaining state of a select box, you have to detect which value is selected and add "selected" to that option:

Code: Select all

<option value="1952" selected>1952</option>

JohnPringle83
New php-forum User
New php-forum User
Posts: 8
Joined: Tue May 03, 2011 5:56 am

Re: setting a value in html select option using php

Postby JohnPringle83 » Tue May 03, 2011 11:07 am

Hello,

Thank you for responding so quickly.

As I do not know javascript I couldn't really understand what to do there.
I have how ever solved part of my problem and used a for loop to generate the options as you will see in the code below.

Code: Select all

<select name="day">
     <?php
        for($i=0;$i<=31;$i++){
        echo "<option value=\"{$i}\"";
        if($i==$_POST['day']){
             echo " selected";
             }
               echo ">";
        if($i==0){
             echo "Day";
        }else{
             echo "{$i}";
        }
        echo "</option>";
        }
     ?>
</select>


However, when you first load the page it says that the variable $_POST['day'] is undefined

Therefore I added this quick fix to the top of my php code

Code: Select all

if(!isset($_POST['submit'])){
   $_POST['day']=0;
}
Last edited by JohnPringle83 on Tue May 03, 2011 11:41 am, edited 2 times in total.

JohnPringle83
New php-forum User
New php-forum User
Posts: 8
Joined: Tue May 03, 2011 5:56 am

Re: setting a value in html select option using php

Postby JohnPringle83 » Tue May 03, 2011 11:38 am

Here is another update to my progress so far:

I have also managed to make the months sticky but as I wanted to have the options displayed as words to the users, I had to tackle it a little differently. I used and array to store the names of the months including the default option and used the array in the loop setting the index to my loop variable.

Code: Select all

<select name="month">
      <?php
             $month=array('Month','January','February','March','April','May','June','July',
                                 'August','September','October','November','December');
             for($i=0;$i<=12;$i++){
                    echo "<option value=\"{$i}\"";
                    if($i==$_POST['month']){
                            echo " selected";
                    }
                   echo ">".$month[$i]."</option>";
             }
      ?>
</select>


again, I had to set a default for $_POST['month'] at the top of my php script and so I now have this at the top of my script:

Code: Select all

if(!isset($_POST['submit'])){
   $_POST['day']=0;
   $_POST['month']=0;
}
Last edited by JohnPringle83 on Tue May 03, 2011 12:27 pm, edited 1 time in total.

JohnPringle83
New php-forum User
New php-forum User
Posts: 8
Joined: Tue May 03, 2011 5:56 am

Re: setting a value in html select option using php

Postby JohnPringle83 » Tue May 03, 2011 11:56 am

I have also now managed to do the Year which is the same as the code for day apart from a few minor changes. I started the loop from the value below the option I wanted to start from which is 1950, so I started the loop at 1949. I set the value 1949 to my default option of "year". I also changed wherever the word day appeared to year.

Code: Select all

<select name="year">
   <?php
      for($i=1949;$i<=2000;$i++){
         echo "<option value=\"{$i}\"";
         if($i==$_POST['year']){
            echo " selected";
         }
         echo ">";
         if($i==1949){
            echo "Year";
         }else{
            echo "{$i}";
         }
         echo "</option>";
      }
   ?>
</select>


again, as expected I had the problem of having the error where $_POST['year'] was undeclared on first load of the page. so I added it to my previous bit of code at the top of my php code and so at the top of my code I now have.

Code: Select all

if(!isset($_POST['submit'])){
   $_POST['day']=0;
   $_POST['month']=0;
   $_POST['year']=1949;
}


As you can see, this time I set it's default value to the same as defined by the loop that I used.


Return to “HTML Basics”

Who is online

Users browsing this forum: No registered users and 2 guests

cron