Help with Condensing Nested getJSON Calls
What I'm making is a product selector where the product has multiple options. I'll add the code at the end if you need it because the code is getting long. I think there has to be a more elegant way to do this. Here is the basic process:
- User searches for an item. The search returns a select box via getJSON.
- User makes a selection. This selection returns up to 5 select boxes (call them 5-1,5-2,etc.) via getJSON.
- User makes selection from 5-1 select box. Horizontal option 5-1-1 is returned (if exists) via getJSON. There is an unknown number of horizontal options that each depend on the horizontal option before it.
- Same as step 3 with all 5 select boxes
I hope that makes sense. I said unknown number of horizontal options because I'm not sure how many, but I think it is max of 4. I don't think there is a way to get around the first two steps, but I'm looking for a way to use the same code each time to select the horizontal options. That is what I'm doing right now, but I have to copy the code inside each successive getJSON call. The PHP is set up to work the same for each horizontal option.
Here is an example of what the 5 select boxes look like:
- <select class="small-input option" name="option4">
- <option value="none">Please Choose from Options Below</option>
- <option rel="" value="~">~ Undecided FABRIC Option</option>
- <option rel="COLAR" value=".AR">.AR FABRIC: Arch</option>
- <option rel="COLCE" value=".CE">.CE FABRIC: Celestial</option>
- </select>
And here is the code that begins at step 1 of my description above:
- $("input[name='searchitems']").result(function(event,data,formatted) {
- var item = "item="+formatted+"&action=loadcatalogs";
- $.getJSON("projecteditor/search.php", item, function(data) {
- var result = data.catalogs;
- $("#catalogselection p").html(result);
-
- // when catalog is selected, add the select boxes for that value in searchitems
- $("select[name='catalogselection']").change(function() {
- var item = $("input[name='searchitems']").val();
- var catalog = $(this).val();
- var lookup = "item="+item+"&catalog="+catalog+"&action=loaddata";
- $.getJSON("projecteditor/search.php", lookup, function(data) {
- $("#option0").html(data.option0);
- $("#option1").html(data.option1);
- $("#option2").html(data.option2);
- $("#option3").html(data.option3);
- $("#option4").html(data.option4);
-
- // see if there is a horizontal option. this is same code for every horizontal
- $(".option").change(function() {
- var option = $(this).val();
- var upfieldset = $(this);
- if ( option != "" ) {
- var catalog = $("select[name='catalogselection']").val();
- var nextoption = $(this).children("option:selected").attr("rel");
- if ( nextoption != "" ) {
- var horizlookup = "nextoption="+nextoption+"&catalog="+catalog+"&action=findhorizontal";
- $.getJSON("projecteditor/search.php", horizlookup, function(data) {
- upfieldset.parent().parent().append(data.out);
-
- });
- } // end of nextoption check
- } // end of option check
- }); // end of option.change
- });
- });
- });
- });