有没有办法获得选择选项的先前值?
之前的值是红色
如果我将其更改为绿色,它将回显所选颜色
如何回显以前的颜色红色?
这是我的代码:谢谢:)
<form action="#" method="post">
<select name="Color">
<option value="Red">Red</option>
<option value="Green">Green</option>
<option value="Blue">Blue</option>
<option value="Pink">Pink</option>
<option value="Yellow">Yellow</option>
</select>
<input type="submit" name="submit" value="Get Selected Values" />
</form>
<?php
if(isset($_POST['submit'])){
$selected_val = $_POST['Color'];
echo "You have selected :" .$selected_val . "<p>";
echo "The previous value is :" ;
}
?>
答案 0 :(得分:2)
使用隐藏字段存储当前值。在表单帖子上,检查当前值是否不同于显示的选定值。
专业提示:始终加密隐藏的字段数据
答案 1 :(得分:1)
将表单中的上一个值存储为隐藏值并获取
<?php
if(isset($_POST['submit'])){
$selected_val = $_POST['Color'];
$previous = $_POST['previous'];
}
?>
<form action="#" method="post">
<select name="Color">
<option value="Red">Red</option>
<option value="Green">Green</option>
<option value="Blue">Blue</option>
<option value="Pink">Pink</option>
<option value="Yellow">Yellow</option>
</select>
<input type="hidden" name="previous" value="<?php echo $selected_val; ?>"/>
<input type="submit" name="submit" value="Get Selected Values" />
</form>
<?php echo "You have selected :" .$selected_val . "<p>";
echo "The previous value is :".$previous ; ?>
答案 2 :(得分:1)
由于您绝不能信任用户提交的内容,另一种方法是从PHP构建表单并在服务器端进行测试。
示例:
<?php
$colors = Array('Red', 'Green', 'Blue', 'Pink', 'Yellow');
?>
<form method="POST">
<select name="Color"><?php
foreach($colors as $color)
{
printf(' <option value="%1$s">%1$s</option>'."\n", $color);
}
?>
</select>
<input type="submit">
</form>
<?php
if (!empty($_POST['Color']))
{
$previous_index = array_search($_POST['Color'], $colors) - 1;
// wrap around if the color is the first
if ($previous_index < 0) {
$previous_index = count($colors);
}
printf(
"You have selected <q>%s</q>, the previous value is: %s\n",
$_POST['Color'],
$colors[ $previous_index ]
);
}
?>
代码显然比教育目的更长。
答案 3 :(得分:1)
这是使用jQuery的另一个解决方案,作为奖励,您可以跟踪整个历史记录,而不仅仅是之前的选择。
我没有测试过PHP部分,但是你明白了。
<form action="#" method="post">
<select name="Color" onChange="updateHistory(this);">
<option value="Red">Red</option>
<option value="Green">Green</option>
<option value="Blue">Blue</option>
<option value="Pink">Pink</option>
<option value="Yellow">Yellow</option>
</select>
<input type="hidden" id="history" name="history" value="">
</div>
</div>
<input type="submit" name="submit" value="Get Selected Values" />
</form>
<?php
if(isset($_POST['submit'])){
$selected_val = $_POST['Color'];
$hist = explode(',',$_POST['history']);
// pop the last value, which is current selection
array_pop($hist);
// get the last value, which now is previous
$last_valaue = end($hist);
echo "You have selected :" .$selected_val . "<p>";
echo "The previous value is :" . $last_valaue ;
echo 'DEBUG:<br>';
var_dump($hist);
}
?>
function updateHistory(el){
var val = $(el).val();
var hist = $('#history').val();
if (hist.length > 0) {hist += ','}
hist += val;
$('#history').val(hist);
}
您可以根据需要在PHP中进一步操作历史数组,例如,如果您需要唯一值,则可以使用array_unique()
(http://php.net/manual/en/function.array-unique.php)