Created
January 31, 2015 01:01
-
-
Save johnbocook/6eb11e3de3db6764536b to your computer and use it in GitHub Desktop.
Populates Drupal 7 Webform select options based on Json object from another system
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Author Note: | |
* Uses a Json object to populate Drupal 7's webform select options. You have to modify webform to accept external select options to be posted. | |
* On remote Json failure, A local Json object is loaded. I have a cron script that updates the Local Json object daily from remote server. | |
*/ | |
//Remove required webform select option | |
$("#edit-submitted-programs-list option[value='NA']").each(function() { | |
$(this).remove(); | |
}); | |
//Get list of programs from Server | |
function getDegrees(div, level) { | |
$.getJSON("https://domain.com", function(data) { | |
$.each(data['DATA'], function(i, b) { | |
if (data['DATA'][i][2] === div && data['DATA'][i][3] === level) { | |
$('#edit-submitted-programs-list') | |
.append($("<option></option>") | |
.attr("value", data['DATA'][i][0]) | |
.text(data['DATA'][i][1])); | |
}; | |
}); | |
}) | |
//On remote populate failure - pull local copy of degrees | |
.fail(function(jqXHR, textStatus, errorThrown) { | |
$.getJSON("/url/of/Json", function(data) { | |
$.each(data['DATA'], function(i, b) { | |
if (data['DATA'][i][2] === div && data['DATA'][i][3] === level) { | |
$('#edit-submitted-programs-list') | |
.append($("<option></option>") | |
.attr("value", data['DATA'][i][0]) | |
.text(data['DATA'][i][1])); | |
}; | |
}); | |
}); | |
}) | |
}; | |
//Listen for Degree of Interest Change | |
$("#edit-submitted-degree-of-interest").change(function() { | |
//Clears Current Option List | |
$("#edit-submitted-programs-list option").each(function() { | |
$(this).remove(); | |
}); | |
switch (document.getElementById('edit-submitted-degree-of-interest').value) { | |
case 'Associates Degree': | |
getDegrees('Category', 'Sub-category'); | |
break; | |
case 'Bachelors Degree': | |
getDegrees('Category', 'Sub-category'); | |
break; | |
case 'Masters Degree': | |
getDegrees('Category', 'Sub-category'); | |
break; | |
}; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment