You can use jQuery's .change()
event, and then use this.value
to check which option was selected and thus run the corresponding API call using an if
statement.
See example below:
$("#hierarchy").change(function() { // Run this function when option selected
let position = this.value;
if(position == "Staff") {
api_login('usa'/*ADD OTHER ARUGMENTS HERE*/);
} else if(position == "Teacher") {
api_login('france'/*ADD OTHER ARUGMENTS HERE*/);
} else if(position == "Student") {
api_login('uk'/*ADD OTHER ARUGMENTS HERE*/);
} else {
console.log("Stop/Don't run any API's");
}
});
function api_login(country, dealer_code, userid_code, actionID = '10', VRN = '', filename = '') { /* ADD country AS A FIRST ARGUMENT */
if (dealer_code === undefined || userid_code === undefined) {
return false;
}
if (actionID === undefined) {
actionID = '10';
}
var string = 'dealerID=' + dealer_code + '&salesExecID=' + userid_code + '&actionID=' + actionID + '&VRM=' + VRN + '&filename=' + filename;
var querystring = country +'.snstest.judgeservice.co.uk/api.php?' + (string); /* CHANGE THIS LINE TO USE country */
var error = [];
$.getJSON(querystring, function(json) {
var response = String(json.code);
var message = String(json.message);
var ident = String(json.identifier);
var username = String(json.accountName);
var siteName = String(json.siteName);
console.log(json);
});
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<label for="Hierarchy">hierarchy</label>
<select name="hierarchy" id="hierarchy" value="">
<option value=""></option>
<option value="Staff">staff</option>
<option value="Teacher"> teacher</option>
<option value="Student">student</option>
</select>
uk.snstest.judgeservice.co.uk/api.php? usa.snstest.judgeservice.co.uk/api.php? france.snstest.judgeservice.co.uk/api.php? so depedning what country
student=uk staff=usa teacher=france
If you plan to add further options to your dropdown, I suggest you use an object with the key
being the option value, and the value of the key is the function to trigger the API call.
Moreover, with the above code, you can use api_login()
with the country passed through. You still need to add your other arguments but as you haven't provided what they are I haven't included them. Also, note that the URL API's which you linked currently don't work with the country appended to the front