PHP下拉问题
My first question, if not appropriate, please ignore.
I have a drop down coded in php which is used to select a value. The value set is 'Off', 0, 1, 2, ... 179 (i.e. 181 values in total).
In the beginning, the drop down shows all 181 values correctly.
After the value is selected from drop down, some other button is pressed to call a mysql procedure. On call completion, page is refreshed.
The drop down is supposed to retain the selected value across page refreshes, which it does. User should be allowed to change the values though.
However, after the first page refresh, If I try to select a different value from the drop down, All I see in there is the previously selected value 181 times.
Below is the code, could someone please help fix it.
I am not a php programmer, just trying to fix a glitch that my resource left in there.
<select style="width: 87px; height:35px;color:#113C83;margin-bottom:0;border-right-width:0;" id="angle1">
<option value="Off">Off</option>
<?php
for($i=0;$i<=179;$i++)
{
if(isset($_SESSION['angle1']))
{
?>
<option value="<?php echo $_SESSION['angle1']; ?>" selected><?php echo $_SESSION['angle1']; ?></option>
<?php
}
else
{ ?>
<option value="<?php echo $i; ?>"><?php echo $i; ?></option>
<?php
}
} ?>
</select>
我的第一个问题,如果不合适,请忽略。 p>
我有 在php中编码的下拉列表,用于选择值。 设置的值为'Off',0,1,2,... 179(即总共181个值)。 p>
在开始时,下拉列表正确显示所有181个值。 p>
从下拉列表中选择值后,按下其他一些按钮即可调用mysql过程。 在通话完成时,页面会刷新。 p>
下拉菜单应该在页面刷新期间保留所选值。 应该允许用户更改值。 p>
但是,在第一页刷新后,如果我尝试从下拉列表中选择一个不同的值,我在那里看到的只是之前的 选择值181次。 p>
下面是代码,有人可以帮忙修复它。 p>
我不是一个php程序员,只是想修复 我的资源留在那里的故障。 p>
h1>
&lt; select style =“width:87px; height:35px; color: #113C83;边距:0;右边框宽度:0;” id =“angle1”&gt;
&lt; option value =“Off”&gt; Off&lt; / option&gt;
&lt;?php
for($ i = 0; $ i&lt; = 179; $ i ++)
{
if(isset($ _ SESSION ['angle1']))
{
?&gt;
&lt; option value =“&lt;?php echo $ _SESSION ['angle1'];?&gt;” 选择&gt;&lt;?php echo $ _SESSION ['angle1']; ?&gt;&lt; / option&gt;
&lt;?php
}
else
{?&gt;
&lt; option value =“&lt;?php echo $ i;?&gt;”&gt;&lt; ?php echo $ i; ?&gt;&lt; / option&gt;
&lt;?php
}
}?&gt;
&lt; / select&gt;
code> pre>
p>
div>
&lt; select style =“width:87px; height:35px; color: #113C83;边距:0;右边框宽度:0;” id =“angle1”&gt;
&lt; option value =“Off”&gt; Off&lt; / option&gt;
&lt;?php
for($ i = 0; $ i&lt; = 179; $ i ++)
{
if(isset($ _ SESSION ['angle1']))
{
?&gt;
&lt; option value =“&lt;?php echo $ _SESSION ['angle1'];?&gt;” 选择&gt;&lt;?php echo $ _SESSION ['angle1']; ?&gt;&lt; / option&gt;
&lt;?php
}
else
{?&gt;
&lt; option value =“&lt;?php echo $ i;?&gt;”&gt;&lt; ?php echo $ i; ?&gt;&lt; / option&gt;
&lt;?php
}
}?&gt;
&lt; / select&gt;
code> pre>
p>
div>
That's because you don't specify the correct condition:
<select style="width: 87px; height:35px;color:#113C83;margin-bottom:0;border-right-width:0;" id="angle1">
<option value="Off">Off</option>
<?php
for($i=0;$i<=179;$i++)
{
if(isset($_SESSION['angle1']) && ($i == $_SESSION['angle1'])) //when you refresh isset($_SESSION['angle1']) will be always true
{
?>
<option value="<?php echo $_SESSION['angle1']; ?>" selected><?php echo $_SESSION['angle1']; ?></option>
<?php
}
else
{ ?>
<option value="<?php echo $i; ?>"><?php echo $i; ?></option>
<?php
}
} ?>
</select>
This is because you are testing the existance of $_SESSION['angle1']
with isset($_SESSION['angle1']
but not that the value it contains matches the current loop counter $i
This tests correctly, assuming that $_SESSION['angle1']
contains the value of the previous selected angle.
This also simplifies your code considerably by using a ternary operator to make that decision
<select style="width: 87px; height:35px;color:#113C83;margin-bottom:0;border-right-width:0;" id="angle1">
<option value="Off">Off</option>
<?php
for($i=0; $i <= 179; $i++) {
$sel = isset($_SESSION['angle1']) && $_SESSION['angle1'] == $i ? 'selected="selected" : '';
echo '<option ' . $sel . ' value="' . $i . '">' . $i .'</option>';
}
?>