Dein Ansatz funktioniert, bedeutet aber einen AJAX-Durchlauf pro Select-Feld. Du kannst das mit JSON in eine einzige Anfrage zusammenfassen. Baue auf der PHP-Seite ein Array aus HTML-Options-Strings für die vier abhängigen Dropdowns und gib es kodiert aus:
$arr = array( "second" => "<option>....</option>.......<option...</option>", // for dropdown 2 "third" => "<option>....</option>.......<option...</option>", // for dropdown 3 "fourth" => "<option>....</option>.......<option...</option>", // for dropdown 4 "fifth" => "<option>....</option>.......<option...</option>" // for dropdown 5 ); echo json_encode($arr); Anschließend füllt auf der Seite eine einzige jQuery-Funktion sie alle:
function loadOptions(){ jQuery.ajax({ success: function(data){ jQuery("select#field_2").html(data["second"]); jQuery("select#field_3").html(data["third"]); jQuery("select#field_4").html(data["fourth"]); jQuery("select#field_5").html(data["fifth"]); } }); } Du brauchst dasselbe kaskadierende Verhalten an anderer Stelle, daher hier eine verallgemeinerte Version, die eine Dropdown-Nummer annimmt. Übergibst du 2, werden Optionen für 3, 4 und 5 zurückgegeben; übergibst du 3, kommen 4 und 5, und so weiter:
function loadOptions(selectNo){ jQuery.ajax({ data: {"selectNo": selectNo}, success: function(data){ switch(selectNo){ case 1: jQuery("select#field_2").html(data["second"]); case 2: jQuery("select#field_3").html(data["third"]); case 3: jQuery("select#field_4").html(data["fourth"]); case 4: jQuery("select#field_5").html(data["fifth"]); } } }); } Und das passende PHP:
$selectNo = $_GET["selectNo"]; $arr = array(); switch($selectNo){ case 1: $arr["second"] = "<option>....</option>.......<option...</option>"; case 2: $arr["third"] = "<option>....</option>.......<option...</option>"; case 3: $arr["fourth"] = "<option>....</option>.......<option...</option>"; case 4: $arr["fifth"] = "<option>....</option>.......<option...</option>"; } echo json_encode($arr); Mehr zu JSON hier: https://labs.jonsuh.com/jquery-ajax-php-json/
Zwei Fehler solltest du in der verallgemeinerten Version beheben: Die switch-Cases brauchen break-Anweisungen, sonst fällt jeder Case in den nächsten durch, und data: {"selectNo",selectNo} ist keine gültige Objektsyntax – es muss {"selectNo": selectNo} heißen.