Jump to content

[SOLVED] Handling <select>, anything besides PEAR?


Gath

Recommended Posts

Hi.

 

The problem is that i have a form that includes a birthdate in a <select> of Years, from 1900 up to 2000.

What i need is when the form isnt properly filled, and returns to itself, for the select to be on the year that was chosen, if any, instead of returning to the default value.

Checked the pear for it, but... too complicated for me :)

Looked around for regex or something, but i'm not finding anything. (altought i'm so tired i might just missed it completly -.-).

 

Any tips, ideas, or something?

 

Thanks.

Link to comment
Share on other sites

Assuming your form uses the POST action ... something like this:

 

<?php
echo "<select name='the_year'>\n";
for ($i=1900;$i<2000;$i++) {
     echo "<option value='. $i. '";
     if ($i == $_POST['the_year']) { echo " selected='selected'";}
     echo ">". $i. "</option>\n";
}
echo "</select>";
><

Link to comment
Share on other sites

Assuming your form uses the POST action ... something like this:

 

<?php
echo "<select name='the_year'>\n";
for ($i=1900;$i<2000;$i++) {
     echo "<option value='. $i. '";
     if ($i == $_POST['the_year']) { echo " selected='selected'";}
     echo ">". $i. "</option>\n";
}
echo "</select>";
><

 

 

Yeah, well, i said i was tired -.- anyway, that wasnt exactaly the my problem... it was a Country list.

I dont even know why i typed the Year thingie...

But thanks for your help.

Link to comment
Share on other sites

Same basic approach for anything. Assuming you have your country list in an array (who doesn't?) you iterate through the elements checking for equality at which point you echo out the selected = 'selected' bit ...

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.