Closed Thread Icon

Topic awaiting preservation: php form select option question (Page 1 of 1) Pages that link to <a href="https://ozoneasylum.com/backlink?for=29322" title="Pages that link to Topic awaiting preservation: php form select option question (Page 1 of 1)" rel="nofollow" >Topic awaiting preservation: php form select option question <span class="small">(Page 1 of 1)</span>\

 
Boudga
Maniac (V) Mad Scientist

From: Jacks raging bile duct....
Insane since: Mar 2000

posted posted 07-01-2007 19:04

Hi all,

I have a website where I allow prepopped text inputs. I have a static select form field with the fifty states. If a state like "TX" is in the $_GET string how can I code the form field to automatically select "TX" out of all 50 states when the page is loaded?

zavaboy
Paranoid (IV) Inmate

From: f(x)
Insane since: Jun 2004

posted posted 07-01-2007 19:21

Something like this:

code:
<?php
$statelist = array('AL', ... the others ... ,'WY');
?>
...
<select name="state">
<?
foreach ($statelist AS $key=>$val){
  if ($_GET['state'] == $val){
    echo "<option value=\"$val\" selected=\"selected\">$val</option>\n";
  }else{
    echo "<option value=\"$val\">$val</option>\n";
  }
}
?>
</select>



I don't usually give code, but I'm feeling nice today! Lucky you.

Boudga
Maniac (V) Mad Scientist

From: Jacks raging bile duct....
Insane since: Mar 2000

posted posted 07-01-2007 19:28

thank you zavaboy

Tyberius Prime
Maniac (V) Mad Scientist with Finglongers

From: Germany
Insane since: Sep 2001

posted posted 07-01-2007 20:27
code:
if ($_GET['state'] == $val){


should read

code:
if (isset($_GET['state'] && $_GET['state'] == $val){


though

Boudga
Maniac (V) Mad Scientist

From: Jacks raging bile duct....
Insane since: Mar 2000

posted posted 07-01-2007 22:19

thanks for the extra bit there TB although I changed it slightly... ;-)

code:
if (isset($_GET['currentstate']) && $_GET['currentstate'] == $val){

Tyberius Prime
Maniac (V) Mad Scientist with Finglongers

From: Germany
Insane since: Sep 2001

posted posted 07-01-2007 23:46

yes.... don't let me type code after long working weekends

« BackwardsOnwards »

Show Forum Drop Down Menu