Friday, 27 July 2012

dynamically add options to a dropdown list that gets values from mysql database

index.php:
<html>
    <head>
    </head>
    <body>
        <select id="dropdown">
            <option value="default" selected="selected">Default option</option>
        </select>
        <script type="text/javascript">
            $.ajax({
              url: 'dropdown-choices.php',
              success: function(data) {
                $('#dropdown').append(data);
              }
            });                
        </script>
    </body>
</html>
dropdown-choices.php:
<?php
    $sql = mysql_query("SELECT * FROM dropdown_choices;");
    while ($data = mysql_fetch_assoc($sql)) {
        echo '<option value="'+$data['value']+'">'+$data['name']+'</option>';
    }
?>
This should do it :)

No comments:

Post a Comment