1

i have two text type forms(id,name) and another form dropbox type(school name)(select option type).

when user enters the id form, the remaining form (name) and the drop box(school name) needs to be filled based on the values in mysql.

filling form text type (name) is not a problem which i am able to do using json and jquery ,but i do not know how to auto select the dropbox.

any tutorials or code snippets suggestions are appreciated.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
Dumb_Shock
  • 1,050
  • 1
  • 13
  • 23
  • 1
    possible duplicate of [jQuery and Auto-populat selects](http://stackoverflow.com/questions/548540/jquery-and-auto-populat-selects) – Marc B Aug 15 '11 at 14:58

3 Answers3

1

I may have misunderstood the question. If you wish the focus to move from the input field to the select you would use .focus(). I would imagine you want to call focus on successful population of the select.

$('#select').focus();

or

$(this).focus();

jQuery Docs : focus()

piddl0r
  • 2,431
  • 2
  • 23
  • 35
1

if your drop box is in xhtml, you can use the "selected" param.

with jquery, you could write semething like :

$("#optiontoSelect").attr("selected","selected");

More info in the jquery doc

Benjamin Dubois
  • 971
  • 7
  • 11
1

This'll get you started:

<?php
$options = Array('red', 'green', 'blue');
$opt_from_db = 'green'; // for example

echo '<select name="fieldName">';
foreach ($options as $opt) {
   $selected = ($opt_from_db == $opt) ? ' selected' : '';
   echo "<option{$selected}>{$opt}</option>";
}
echo '</select>';
?>

The key is to write the selected attribute (in some form or another) into the programmatically-rendered HTML.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055