By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,045 Members | 1,305 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,045 IT Pros & Developers. It's quick & easy.

How to set a drop down option by picking value from the database.

P: 7
Is there any way to get the drop down value populated with the value from the database (Mysql) using php code.

Following is one of the table row which contains the drop down and texfield :

Expand|Select|Wrap|Line Numbers
  1.     <form method="POST" action="processIdce.php">
  3.     <table border=1>
  4.     <?php
  5.     echo "<tr><td>Has the Checklist Reviewer reviewed the IDCE code?</td>";
  6.     echo "<td><select name=\"d_response1\" id=\"d_response1\"><option value=\"Yes \">Yes</option><option value=\"No\">No</option></select></td>";
  7.     echo "<td><textarea name=\"d_comments1\" id=\"d_comments1\" value></textarea></td>";
  8.     echo "<td><select name=\"r_response1\" id=\"r_response1\"><option  value=\"Yes\">Yes</option><option value=\"No\">No</option></select></td>";
  9.     echo "<td><textarea name=\"r_comments1\" id=\"r_comments1\"></textarea>  </td></tr>";
  10.     ?>
  11.     </table>
  12.     <input type="submit" name="submit" value="Submit"/>
  13.     </form>
Now entering the data and pressing submit the data will be fed into the database and same table row will be presented on "processIdce.php" as shown in the above code but with the drop down menu showing the option selected when the data is submitted.Is there any way out to do this.

Any help regarding the same will be appreciated..:)
Jan 11 '12 #1
Share this Question
Share on Google+
2 Replies

Expert Mod 10K+
P: 12,359
You can use the selected attribute of the option tag to mark it as the choice.
Jan 11 '12 #2

P: 7
I solved the problem myself by putting in the condition in option tag depending upon which we will echo out the String 'selected=selected'.
Suppose choice1 is drop down list while $data_count is the total number of rows returned by firing mysql_query while $data_status is the data picked for the drop down from the database.
then u can use the following code :
Expand|Select|Wrap|Line Numbers
  1. <select name="choice1"><option value ='Yes' if($data_count!=0 && $data_status='Yes')echo 'selected=selected'>Yes</option>
  2. <option value ='No' if($data_count!=0 $data_status='No')echo 'selected=selected'>No</option>
  3. </select>
Jan 17 '12 #3

Post your reply

Sign in to post your reply or Sign up for a free account.