PHP:设置选择选项

时间:2014-04-14 06:22:18

标签: php select option

我已经尝试了很多,以便在我们浏览的所有网页上保留所选的选项值,但我没有成功:(

任何人都可以弄清楚我的代码出了什么问题吗?

<form name="area_form" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST" >
<?php

    $sql = "select *from city";
    $loop = mysql_query($sql)
    or die ('cannot run the query because: ' . mysql_error());       
echo "<select name='areaa' onchange='window.location.href=this.value'>"; 
while($row = mysql_fetch_array($loop)) 
{
if($_POST['areaa']==$row['area']) 
    {
     echo "<option selected  VALUE=\"http://example.com\">".$row['area']."</option>"; 
    }
     else
     { 
     echo "<option  VALUE=\"http://example.com\">".$row['area']."</option>";
     } 
}
 echo "</select>"; 

?>

2 个答案:

答案 0 :(得分:0)

答案 1 :(得分:0)

$areaa = isset($_POST['areaa'])?$_POST['areaa']:"";

while($row = mysql_fetch_array($loop)) 
{

 echo "<option  VALUE=\"http://example.com\" $areaa==\"http://example.com\"?'selected=selected' : ''  >".$row['area']."</option>"; 
     echo "<option  VALUE=\"http://example2.com\" $areaa==\"http://example2.com\"?'selected=selected' : '' >".$row['area']."</option>";

}