提交给SELF时'记住'表单下拉列表值? [英] 'Remember' form drop list value when submitting to SELF?
问题描述
无论何时将表单提交给自己,我都知道如何记住某些表单值,在这种情况下是因为图片上传功能需要将表单提交给自己。我只是想要它,如果用户填写了所有字段,然后上传图像,表单不会重置(清除)。
我已经解决了这个问题在常规字段和复选框,如下所示:
< input type =textname =headlineid =headline value =<?php echo @ $ _ POST ['headline'];?>>
但是如何使用删除列表来做到这一点?或单选按钮?在'SELECT'列表中没有值选项,即使我已经尝试在SELECT语句中写入值。没有工作!
那么,我该如何使用PHP(OR JAVASCRIPT)设置SELECT(下拉列表)值?
如果您需要更多的输入,请告诉我,谢谢! 解决方案
选项发布到您的发布价值,并单独处理。只需在循环中打印出您的选项,并根据以前发布的值对每个值进行测试。如果匹配,请将 selected
添加到该特定选项的属性中。
$ color = $ _POST [colors];
$ colors = array(red,green,blue);
< select name =colors>
<?php foreach($ colors as $ option){?>
< option<?php print($ option == $ color)? selected:; ?>>
<?php print $ option; ?>
< / option>
<?php}?>
< / select>
I know how to 'remember' some form values whenever submitting the form to itself, in this case because of a picture upload function which requires the form to be submitted to itself. I simply want it so that if the user has filled out all fields and then uploads an image, the form doesn't get resetted (cleared).
I have solved this in regular fields and checkboxes like this:
<input type="text" name="headline" id="headline" value="<?php echo @$_POST['headline'];?>">
But how can I do this with drop lists? or radio buttons? There is no value option in a 'SELECT' list, even though I have tried writing in value anyways in the SELECT statement. Didn't work!
So, how can I set the SELECT (drop down lists) value with PHP (OR JAVASCRIPT) ?
If you need more input let me know, thanks!
For selects, you need to compare each option to your posted value, and handle it individually. Simply print out your options in a loop, and test each value against the value was was previously posted. If it maches, add selected
to the attributes of that particular option.
$color = $_POST["colors"];
$colors = array("red","green","blue");
<select name="colors">
<?php foreach ($colors as $option) { ?>
<option<?php print ($option == $color) ? " selected" : ""; ?>>
<?php print $option; ?>
</option>
<?php } ?>
</select>
这篇关于提交给SELF时'记住'表单下拉列表值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!