如何在php中设置<select>的选定选项

时间:2015-07-07 14:02:36

标签: php html

我需要保存我的信息,在提交按钮后我需要输出所有信息,我需要选择的选项在我的表单上提交按钮后保持选中状态。 我的代码输出信息,但输出包含状态的数字,我需要将状态输出为字符串 &LT; PHP $ name = $ _POST ['name']; $ state = $ _POST ['state']; $ 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 =&gt; $ v) echo $ p; $ x = explode(“,”,$ v); ?&GT;     &lt; form action =“”method =“post”&gt;         &lt; input name =“name”value =“&lt;?php echo $ x ['0'];?&gt;”/&gt;         &lt; select name =“state”&gt;             &lt; option value =“0”&gt;统计&LT; /选项&GT;             &LT; 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($省为$ k =&gt; $ v)             echo“&lt; option value ='”。($ k + 1)。“'&gt; $ v&lt; / option&gt;”;             ?&GT;         &lt; input type =“submit”name =“submit”value =“trimite”/&gt;         &LT; /选择&GT;     &LT; /形式&GT;

2 个答案:

答案 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>";
}