How to populate combo on load form !
hi guys, i am new jquery and hope someone could help me with .
i am trying to configure a simpe combobox and i thought to populate it in the beginning the the form is loaded (i guess this is the best idea but any advice are welcome).
unfortunately the combo is not loaded when the form is load , here is my code :
html
- <tr>
- <td><label for="labelCountry">Stato</label></td>
- <td>
- <select id="country" name="country">
- <option value="0" selected>Seleziona stato ...</option>
- <option value="1" selected>Sele ...</option>
- <option value="2" selected>...</option>
- </select>
- </td>
- </tr>
javascript :
- $(function()
- {
- $('#country').focus(function() {
- $.getJSON('combobox.php',{"id":$(this).val()},function(data) {
- $('#country option').remove();
- $.each(data,function(i,item) {
- $('#country').append('<option value="'+item.id+'">'+item.value+'</option>');
- });
- });
- });
- });
php:
- $sql = "SELECT iso,printable_name FROM country;";
- /* exec query */
- [ ... ]
- $array = array();
- foreach ($res as $key => $val) {
- $array[] = array('id' => $val['iso'],'value' => $val['printable_name']);
- }
-
- echo json_encode( $array );
Thanks for answering !!
N