user696734
user696734

Reputation: 63

How make dropdowns in php pages?

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.

Upvotes: 0

Views: 421

Answers (1)

Kaii
Kaii

Reputation: 20540

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

Upvotes: 1

Related Questions