fill second (or more) with data from a json request. and yes, those are the breeds of my pets.
-- select value -- dog cat domestic medium hair
jquery $(document).ready(function(){ $(select#category).change(function(){ // post string var post_string = type= + $(this).val(); // send the request and update sub category dropdown $.ajax({ type: post, data: post_string, datatype: json, cache: false, url: 'json.php', timeout: 2000, error: function() { alert(failed to submit); }, success: function(data) { // clear all options from sub category select $(select#sub_category option).remove(); // fill sub category select $.each(data, function(i, j){ var row = + j.text + ; $(row).appendto(select#sub_category); }); } }); }); });
html -- select value -- dog cat -- select first value --
json ajax script, json.php $json = array(); if ($_post['type'] == 1) { $json[] = array( 'value' => '1', 'text' => 'staffordshire bull terrier' ); $json[] = array( 'value' => '2', 'text' => 'labrador retriever/american pit bull mix' ); $json[] = array( 'value' => '3', 'text' => 'german short hair pointer' ); } elseif ($_post['type'] == 2) { $json[] = array( 'value' => '4', 'text' => 'domestic medium hair' ); } echo json_encode($json);
