如何使php页面下拉? [英] How make dropdowns in php pages?

查看:119
本文介绍了如何使php页面下拉?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个包含多个字段的帐户。这些是:姓名,地址,电话,秘密问题等。这工作正常,但我在编辑页面时遇到问题。如何重新填充秘密问题下拉字段?
请指导我的html代码如下所示:

 < select name =securityqustionclass =securityqustion > 
< option value =<?php echo $ row ['scruity_question'];?>选择>
<?php echo $ row ['scruity_question'];?>< / option>
< option class =optionvalue =什么是您的昵称>您的昵称是什么< / option>

在上面的代码中,重复选择的问题。

解决方案

你需要从数组中构建它并检查迭代值是否是所需值,因此你可以添加 selected

该代码生成列表并选择< option> ,它与<$ c
$ b $ $ p $ < select name =securityqustionclass:$ c> $ row ['scruity_question']

=securityqustionid =security_qustion>
<?php
$ questions = array(你的昵称是什么,你的第一所学校是什么,你的猫的名字是什么);
forand比较每个($ questions为$ question){
if($ question == $ row ['scruity_question'])
$ selected =selected
else
$ selected =;
echo< option value = \{$ question} \{$ selected}> {$ question}< / option> \\\
;
}
?>
< / select>

注意

<将安全问题存储为字符串是一个坏主意 - 当您更改问题的拼写时,它与存储在数据库中的内容不匹配。最好将问题放入单独的数据库表中,并使用问题的ID作为参考。



SIDENODE 来自Damien Pirsy(足够重要包括在答案中)

作为安全问题的昵称并不安全:我的所有朋友都可以通过猜测来为我的帐户申请一个新密码......第一所学校也是,即使是我的亲戚也会知道这一点!:D即使在社会工程学领域,也可以使用真正难以猜测的东西;或更好,根本不使用安全问题 - 达米恩皮尔斯


I made a form to create an account which contains several fields. Those are: name,address,phone,secret question etc. This is working fine, but I face a problem in making the edit page. How can I repopulate the secret question dropdown field? Please guide my html code is like following:

<select name="securityqustion"  class="securityqustion"  >
<option value="<?php echo $row['scruity_question'];?>" selected >
<?php echo   $row['scruity_question'];?></option>
<option class="option"  value="what is your nickname">what is your nickname</option>
<option value="what is your first school">what is your first school</option>
</select>

In the above code the selected question is repeated.

解决方案

you need to build that from an array and check if the iterated value is the desired one, so you can add selected to it.

This code generates the list and selects the <option> which matches the $row['scruity_question']

<select name="securityqustion"  class="securityqustion" id="security_qustion">
<?php
  $questions = array("what is your nickname", "what is your first school", "whats your cats name");
  forand compare each ($questions as $question) {
    if ($question == $row['scruity_question']) 
      $selected = "selected"
    else 
      $selected = "";
    echo "<option value=\"{$question}\" {$selected}>{$question}</option>\n";
  }
?>
</select>

NOTE

Storing the security question as a string is a bad idea - when you change the spelling of a question it doesn't match whats stored in the database. Better put the questions into a seperate database table and use the ID of a question for references.

SIDENODE from Damien Pirsy (important enough to be included in an answer)

nickname as a security question is not really safe: all my friends would be able to request a new password for my account just by "guessing" that...First school also, even my relatives would know that!:D Use something which is really difficult to guess, even with social engineering; or better, don't use security questions at all – Damien Pirsy

这篇关于如何使php页面下拉?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆