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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Gaurav Pruthi
    New Member
    • Dec 2011
    • 7

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

    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 :

    Code:
        <form method="POST" action="processIdce.php">
    
        <table border=1>
        <?php
        echo "<tr><td>Has the Checklist Reviewer reviewed the IDCE code?</td>";
        echo "<td><select name=\"d_response1\" id=\"d_response1\"><option value=\"Yes \">Yes</option><option value=\"No\">No</option></select></td>";
        echo "<td><textarea name=\"d_comments1\" id=\"d_comments1\" value></textarea></td>";
        echo "<td><select name=\"r_response1\" id=\"r_response1\"><option  value=\"Yes\">Yes</option><option value=\"No\">No</option></select></td>";
        echo "<td><textarea name=\"r_comments1\" id=\"r_comments1\"></textarea>  </td></tr>";
        ?>
        </table>
        <input type="submit" name="submit" value="Submit"/>
        </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.ph p" 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..:)
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    You can use the selected attribute of the option tag to mark it as the choice.

    Comment

    • Gaurav Pruthi
      New Member
      • Dec 2011
      • 7

      #3
      I solved the problem myself by putting in the condition in option tag depending upon which we will echo out the String 'selected=selec ted'.
      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 :
      Code:
      <select name="choice1"><option value ='Yes' if($data_count!=0 && $data_status='Yes')echo 'selected=selected'>Yes</option>
      <option value ='No' if($data_count!=0 $data_status='No')echo 'selected=selected'>No</option>
      </select>

      Comment

      Working...