NameSilo

How to autopopulate a form w/ PHP

Spaceship Spaceship
Watch

obsidianchrysalis

Established Member
Impact
0
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....
 
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
GoDaddyGoDaddy
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>
 
0
•••
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.
 
0
•••
thanks...this helps out alot.
 
0
•••
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>';
?>

I think that's about it what you need for loong lists of options.
 
0
•••

We're social

Unstoppable Domains
Domain Recover
NameMaxi - Your Domain Has Buyers
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back