问题描述
我正在使用php建立一个更改分类页面。
I am using php to build a "change classifieds" page right now.
我使用Mysql作为数据库。
I use Mysql as a db.
目前我使用PHP获取有关分类的所有mysql信息,然后我输出如下:
Currently I use PHP to fetch all mysql information about the classified, and then I output it like this:
$table.="
<select name='year' id='year'>
<option id='2000' value='2000'>2000</option>
<option id='2001' value='2001'>2001</option>
<option id='2002' value='2002'>2002</option>
</select>";
echo $table;
我有一个图片上传工具,可以将页面提交给自己,同时上传图片,并在页面底部显示。问题是,无论何时完成,用户必须再次填写所有信息,因为它们被遗忘。
I have a picture upload tool which submits the page to itself, and at the same time uploads the picture, and shows it at the bottom of the page. The problem is, whenever this is done, the user must fill in all information again, because they are "forgotten".
我知道输入类型=文字你可以做以下事情:
I know about input type="text" where you simply can do something like this:
<input type="text" name="text" value="<?php echo $_POST['text'];?>">
但选择呢?收音机?等等?
but what about selects? Radios? etc?
我该怎么办?
谢谢
推荐答案
对于选择,您需要循环访问数据并检查值是否等于所选值(至少这是最容易做到的事情)。但你可以使用复选框/收音机看到的类似技术。
For selects, it is a bit more intense you need to loop through the data and check if the value equals the selected value (at least that is the easiest to do). But you could apply a similar technique as seen with the checkbox / radio.
<input type="radio" name="radio1" value="1" <?php echo (!empty($_POST['radio1']))?'checked':''?>>
<input type="checkbox" name="chkbox1" value="1" <?php echo (!empty($_POST['chkbox1']))?'checked':''?>>
由于您在创建表格,因此PHP侧面是代码:
Since you are creating the table PHP side here is the code:
$years = array('2000', '2001', '2002');
$table .= "<select name='year' id='year'>";
foreach ($years as $year) {
$table .= "<option id='" . $year . "' value='" . $year . "' " .
(($_POST['year'] == $year)?'selected':'') . ">" . $year . "</option>\n";
}
$table.="</select>";
应该让您选择正确的陈述。的?和:组成三元运算符,它是一个缩短的if / else语句。
Should get you on the right track for select statements. The ? and : make up the ternary operator which is a shortened if / else statement.
这篇关于如何使表单元素“记住”选择?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!