php - Dynamic Select Dropdown Wordpress By advanced custom field
17
This is my code for Multiple Dropdown.
I have two dropdown, One for City and other for Area.
So when someone select a city, then Area dropdown should automatically populate.
THis is the url
<?
$field_key = "field_570e68df39304";
$field = get_field_object($field_key);
if( $field == 'pune' )
{
$field_key = "field_570e68df39304";
$field = get_field_object($field_key);
if( $field )
{
echo '<select name="city" id="city">';
foreach( $field['choices'] as $k => $v )
{
echo '<option value="' . $k . '">' . $v . '</option>';
}
echo '</select>';
}
}
elseif ( $field == 'akola' )
{
$field_key = "field_570e691b39305";
$field = get_field_object($field_key);
if( $field )
{
echo '<select name="city" id="city">';
foreach( $field['choices'] as $k => $v )
{
echo '<option value="' . $k . '">' . $v . '</option>';
}
echo '</select>';
}
}
else
{
?>
<select >
<option>Select Area</option>
</select>
<?
}
?>
Answer
Solution:
In this case you will probably have to use some ajax in your code so that when you change the value of the first drop down menu, it triggers an ajax call. To achieve this you would have to include some javascript like this
What is happening there is that when your city drop down is changed, it triggers an Ajax request to get all related areas. Ajax in Wordpress is done through the admin-ajax system. You can read more on that here: https://codex.wordpress.org/AJAX_in_Plugins
In your functions.php file you could add the following to register an Ajax call to retrieve the city areas
That should help you get what you are looking for.