PHP dependent dropdown based on file columns
Hi, i have a dependent dropdown.
When you select county, you get the constituency and when you select the constituency you get the ward.
I am able to get the constituency from the county but i get an error Notice: Undefined index: Please choose a constituency in C:\xampp\htdocs\ramani_dsl\lidepot\dropdown.php on line 44 {"county":["MOMBASA","KWALE","KILIFI","TANA RIVER"]} when trying to get the ward.
My code looks like this :
<?php
// read the CSV file in the $makes_models_years array
$makes_models_years = array();
//$uploads_folder = wp_upload_dir()['basedir'];
chdir(__DIR__);
$file = fopen("./resources/polling_data.csv","r");
$firstline = true;
while (($line = fgetcsv($file)) !== FALSE) {
if ($firstline) {
$firstline = false;
continue;
}
$makes_models_years[$line[0]][$line[1]][] = $line[2];
}
fclose($file);
// setup the initial array that will be returned to the the client side script as a JSON object.
$return_array = array(
'county' => array_keys($makes_models_years),
'constituency' => array(),
'ward' => array(),
'current_county' => false,
'current_constituency' => false
);
// print_r($return_array);
// collect the posted values from the submitted form
$make = key_exists('county', $_POST) ? $_POST['county'] : false;
$model = key_exists('constituency', $_POST) ? $_POST['constituency'] : false;
$year = key_exists('ward', $_POST) ? $_POST['ward'] : false;
// populate the $return_array with the necessary values
if ($make) {
$return_array['current_county'] = $make;
$return_array['constituency'] = array_keys($makes_models_years[$make]);
if ($model) {
$return_array['current_constituency'] = $model;
$return_array['ward'] = $makes_models_years[$make][$model];
if ($year) {
$return_array['current_ward'] = $year;
}
}
}
// encode the $return_array as a JSON object and echo it
echo json_encode($return_array);
?>
My csv looks like https://onedrive.live.com/edit.aspx?resid=53D2116F2F9AA49!225144&ithint=file%2cxlsx&authkey=!AOdTqj8O4Py0PQ0
Any pointers on how to get the ward from the ward will be appreciated.
Please or to participate in this conversation.