3

3个带值的下拉菜单

$options1 = array( 1=>'= Equals', '≠ Does not Equal', '> Is greater than', '≥ Is greater than or equal to', '< Is less than', '≤ Is less than or equal', '∋ Contains', '∌ Does not contain');

$counter_maximum = 3;

while ($counter < $counter_maximum){

echo '
<select name="entry_id_selector[]">';
foreach ( $options1 as $i1=>$opt1 ) :
echo '<option value="' .$i1 .'"';

foreach ($entry_id_selector_topic as $entry_id_selector_i=>$entry_id_selector_topic_value ) :
echo (($i1 == $entry_id_selector_topic_value)? 'selected' : "");
endforeach;

echo '>';
echo $opt1 .'</option>';
endforeach;
echo '</select>';

$counter++;
}

用户选择了一些值

使用用户选择的值设置变量(数组)(此代码高于所有其他代码;位于页面顶部)

$entry_id_selector_topic = $_POST['entry_id_selector'];

默认值(页面首次加载后)是 $options1 或= Equals. 用户单击提交按钮后,我想记住用户选择的值。

但是使用此代码是以下行为:

1) 如果用户仅在一个下拉菜单中更改/选择值,则单击提交后,所有下拉菜单中的值都会更改为用户选择的值。

2) 如果用户在多个下拉菜单中更改值,则所有菜单中的值都会更改/选择为值更改的最后一个下拉菜单的值。

实验时试图检查$entry_id_selector_topic_value并在下面$entry_id_selector_topic = $_POST['entry_id_selector'];放置此代码

foreach ($entry_id_selector_topic as $entry_id_selector_i=>$entry_id_selector_topic_value ) {
echo $entry_id_selector_topic_value .' $entry_id_selector_topic_value<br>';
}

$entry_id_selector_topic_value正是从每个下拉菜单中选择的值。

需要更正(更改)什么以记住用户在每个下拉菜单中选择的值?

4

2 回答 2

3

这是工作代码

$counter = 0;
$counter_maximum = 3;

while ($counter < $counter_maximum){
$entry_id_selector_topic = $_POST['entry_id_selector'][$counter];

echo '
<select name="entry_id_selector[]">';
foreach ( $options1 as $i1=>$opt1 ) :
echo '<option value="' .$i1 .'"';

echo (($i1 == $entry_id_selector_topic)? 'selected' : "");

echo '>';
echo $opt1 .'</option>';
endforeach;
echo '</select>';

$counter++;
于 2013-06-22T14:53:14.043 回答
0
foreach ($entry_id_selector_topic as $entry_id_selector_i=>$entry_id_selector_topic_value ) :
echo (($i1 == $entry_id_selector_topic_value)? 'selected' : "");
endforeach;

这部分不正确,你的名字应该是

<select name="entry_id_selector[$counter]">

如果不可能有多个值,您应该添加另一个数组,那么您应该这样做

if ($entry_id_selector_topic[$counter] == $i) echo ' selected ';

给,这么长的名字是不可取的。你也可以写

foreach ($entry_id_selector_topic as $entry_id_selector_topic_value ) :
于 2013-06-22T14:05:26.927 回答