Last active
September 28, 2020 08:05
-
-
Save dave-mills/8dbaaffbfb016fcf4a9f8ecc50e89e5e to your computer and use it in GitHub Desktop.
Example Callback function for Ultimate Member dropdown field
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
function getCities() { | |
//get the value from the 'parent' field, sent via the AJAX post. | |
$choice = $_POST['parent_option']; | |
//Depending on the value of $choice, return a different array. | |
switch($choice) { | |
case "France": | |
$cities = [ | |
"Paris" =>"Paris", | |
"Marseille" => "Marseille", | |
"Lyon" => "Lyon" | |
]; | |
break; | |
case "Spain": | |
$cities = [ | |
"Madrid"=>"Madrid", | |
"Barcelona"=>"Barcelona" | |
]; | |
break; | |
default: | |
//code to do something if other options are not selected (throw an error, or set $cities to a default array) | |
$cities = ["no city"]; | |
} | |
return $cities; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
In case it helps anyone else coming here to figure this out:
The parent and child fields must be “Dropdown” field types; not just any field can provide a parent value
If you need to populate a dropdown based on a field that is not a type that supports being a "parent option", you’ll need to write some custom js to capture the input value and assign it to a dummy/hidden single-option dropdown that can be used as the parent - e.g.:
$domain = $_POST['parent_option'];