php – 当我返回页面时,下拉菜单不会保持选中状态

前端之家收集整理的这篇文章主要介绍了php – 当我返回页面时,下拉菜单不会保持选中状态前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
任何想法,为什么以下脚本的下拉菜单不会保留在选定的值上:
if (isset($_POST['Submit1'])) {
     $frequency = $_POST['frequency'];
     $fileName = 'ConfigurationFile.txt';

     $lines = file($fileName);

     $lines[0]="Center_frequency= ". $frequency."\n";

     file_put_contents($fileName,implode($lines));

 } else {

     $fileName = 'ConfigurationFile.txt';

     $lines = file($fileName);

     $frequency=explode(" ",$lines[0]);
     $frequency=$frequency[1];
 }
 ?>

但如果我把我的号码放在引号意义上

<?PHP

 if (isset($_POST['Submit1'])) {
     $frequency = $_POST['frequency'];
     $fileName = 'ConfigurationFile.txt';

     $lines = file($fileName);

     $lines[0]="Center_frequency= ".'"'. $frequency.'"'."\n";

     file_put_contents($fileName,implode($lines));

 } else {

     $fileName = 'ConfigurationFile.txt';

     $lines = file($fileName);

     $frequency=explode("\"",$lines[0]);
     $frequency=$frequency[1];
 }
 ?>

它会像魅力一样工作,意味着选定的值会保留,下拉菜单不会转到列表中的第一个值.
这是下拉菜单HTML代码

<form action="test.PHP" name="Calculation" method="post">
     Center Frequency: 
     <select name="frequency" >

     <?PHP $attr= 'selected="selected"'; ?>

    <option value="4.463" <?PHP echo $frequency  == '4.463' ? $attr :  ''; ?>>4.463</option>
    <option value="5.2625" <?PHP echo $frequency  == '5.2625' ? $attr : ''; ?>>5.2625 </option>
    <option value="9.33"   <?PHP echo $frequency  == '9.33' ? $attr : ''; ?>>9.33 </option>
    <option value="13.5"   <?PHP echo $frequency  == '13.5' ? $attr : ''; ?>>13.5 </option>
    <option value="16.15"  <?PHP echo $frequency  == '16.15' ? $attr : ''; ?>>16.15 </option>
    <option value="24.525" <?PHP echo $frequency  == '24.525' ? $attr : ''; ?>>24.525</option>
    <option value="26.275" <?PHP echo $frequency  == '26.275' ? $attr : ''; ?>>26.275</option>

   </select>

    <Input Type = "Submit" Name ="Submit1" Value = "Save Parameters">

    </form>
$fileName = 'ConfigurationFile.txt';

        $lines = file($fileName);

        $frequency=explode(" ",$lines[0]);
        $frequency=$frequency[1];

        var_dump($frequency);
        //string(5) "13.5 " <== space

$fileName = 'ConfigurationFile.txt';

        $lines = file($fileName);

        $frequency=explode(" ",$lines[0]);
        $frequency=trim($frequency[1]); //trim function

http://php.net/manual/pl/function.trim.php

猜你在找的PHP相关文章