答案 0 :(得分:2)
请检查以下代码: -
<?php
if(isset($_POST) && !empty($_POST)){
$name = $_POST['name'];
$state = $_POST['state'];
// I don't know what you are doing here through this code
/* $file = $name.".txt";
$txt = $name.",".$state.",";
$wrt = file_put_contents($file, $txt, FILE_APPEND);
$get_file = file_get_contents($file);
$info = array($get_file);
foreach($info as $k => $v)
echo $p;
$x = explode(",",$v); */
}
?>
<form action="" method="post">
<input name="name" value="<?php if (isset($name)){echo $name;}?>"/>
<select name="state">
<option value="0"> Stat</option>
<?php
$province = array("AA", "AE" ,'AK" , "AL" , "AP" , "AR" , "AS" , "AZ" , "CA" , "CO" , "CT" , "DC" , "DE" , "FL" , "FM" , "GA" , "GU" , "HI" , "IA" , "ID" , "IL" , "IN" , "KS" , "KY" , "LA" , "MA" , "MD" , "ME" , "MH" , "MI" , "MN" , "MP" , "MO" , "MS" , "MT" , "NC" , "ND" , "NE" , "NH" , "NJ" , "NM" , "NV" , "NY" , "OH" , "OK" , "OR" , "PA" , "PR" , "PW" , "RI" , "SC" , "SD" , "TN" , "TX" , "UT" , "VA" , "VI" , "VT" , "WA" , "WI" , "WV" , "WY');
foreach($province as $k=> $v){
if(isset($state) && $state == $k){
echo "<option value='".$k."' selected = 'selected'>$v</option>";
}else{
echo "<option value='".$k."'>$v</option>";
}
}
?>
</select>
<input type ="submit" name="submit" value="trimite" style = "margin-top:30px;"/>
</form>
答案 1 :(得分:0)
这样的事情我通常会这样做:
foreach($province as $k => $v) {
$sel = ($state == $k) ? " selected='selected'" : "";
echo "<option value='".($k+1)."' {$sel}>{$v}</option>";
}