| | |||||
| ||||||||
| Programming PHP, Perl, Ruby on Rails, AJAX, HTML, XHTML, CSS, JavaScript, MySQL and any other coding topics. |
![]() |
| | LinkBack | Thread Tools |
| | THREAD STARTER #1 (permalink) |
| New Member Join Date: Mar 2005
Posts: 24
![]() | how to autopopulate a form w/ PHP hey all. i have a bit of a problem getting a form to populate with information from a list of $_GET variables. i can get text fields to populate with the values from the variables, but i can't get <SELECT> fields to austo select to the values. any help would be appreciated.... |
| |
| | #2 (permalink) |
| NamePros Webmaster ![]() ![]() ![]() Join Date: Feb 2003
Posts: 12,930
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() | Can you share how you currently display the <select> options in your PHP script? Are the options generated dynamically or are they static? There are a few ways to do it. Here's a basic way, <select name="field1"> <option value="myvalue1" <? if ($_GET['field1'] == 'myvalue1') echo 'selected';?>> <option value="myvalue2" <? if ($_GET['field1'] == 'myvalue2') echo 'selected';?>> </select> |
| |
| | #3 (permalink) |
| NamePros Expert Join Date: Nov 2003 Location: Scotland
Posts: 5,069
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() | yes the way -RJ- has suggested is the way to go about it even if they are dynamically created you can still do it the way -RJ- mentions you just use the same variable where the value is anjd also in the if statement such as:- <option value="<?PHP echo $variable1 ?>" <?PHP if ($_GET['field1'] == $variable1) echo 'selected';?>> Just a note however although short tags work (which is what -RJ- used) it is best practice not to use them so that your code is as portable as possible. |
| |
| | #5 (permalink) |
| NamePros Member Join Date: Nov 2005 Location: the Netherlands
Posts: 127
![]() | Code: <?php
$options[] = "myvalue1";
$options[] = "myvalue2";
$options[] = "myvalue3";
$options[] = "myvalue4";
echo '<select name="field1">';
foreach($options as $option) {
if($_GET['field1']==$option) { $blaat = ' selected="selected"'; } else { $blaat = ''; }
echo '<option value="'.$option.'"'.$blaat.'>'.$option.'</option>';
}
echo '</select>';
?>
__________________ accepts paypal and cookies |
| |
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| |
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| PHP Form help - beginner :( | neilfahey | Programming | 6 | 11-09-2005 07:59 PM |
| PHP search form | Barrucadu | Programming | 18 | 08-11-2005 10:12 AM |
| Populate a dropdown for PHP form | static1635 | Programming | 6 | 08-10-2005 05:32 AM |