javascript - Update select list with variable values when other select option is selected? -


i have php array called $programdates_items, contains values (school programs) multiple dates inside each program. have form select input "program of interest", options representing each program.

i want build function when program of interest selected, select input called "dates" updated filled options made of corresponding date values programdates.

the problem don't know how go marrying php , javascript in order accomplish this.

below have included output $programdates_items array, , section of form array pertains to.

$programdates_items = array ( [animation] => array ( [january 1, 2018] => january 1, 2018 [april 28, 2018] => april 28, 2018 ) [game design] => array ( [february 20, 2018] => february 20, 2018 [june 28, 2018] => june 28, 2018 [october 20, 2018] => october 20, 2018 )   <select id="program_of_interest" name="program_of_interest" required="">     <option value="">program of interest</option>     <option value="animation">animation</option>     <option value="gamedesign">game design</option> </select>  <select id="start_date" name="start_date" required="">     <option value="">start date</option>             </select> 

i want something this...

$( "#program_of_interest" ).change(function() {     $selected_program = $("#program_of_interest").val();      if ($select_program == "animation") {       ~loop through $programdates_items array , output 'animation' array values <option>~     } else {       ~else~     } }); 

how update "start_date" select input program dates $programdates_items php array based on selected option "program_of_interest" input?

changing structure of $programdates_items object like:

var values = {animation: ["january 1, 2018", "april 28, 2018"],        gamedesign: ["february 20, 2018", "june 28, 2018", "october 20, 2018"]}; 

the solution problem is:

$( "#program_of_interest" ).on('change', function(e) {     $("#start_date").empty();     (values[$(this).val()] || ['start date']).foreach(function(ele, idx) {         $("#start_date").append($('<option/>', {text: ele, value: ele}));     }) }); 

var values = {animation: ["january 1, 2018", "april 28, 2018"], gamedesign: ["february 20, 2018", "june 28, 2018", "october 20, 2018"]};    $( "#program_of_interest" ).on('change', function(e) {      $("#start_date").empty();      (values[$(this).val()] || ['start date']).foreach(function(ele, idx) {          $("#start_date").append($('<option/>', {text: ele, value: ele}));      })  });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>    <select id="program_of_interest" name="program_of_interest" required="">      <option value="">program of interest</option>      <option value="animation">animation</option>      <option value="gamedesign">game design</option>  </select>  <select id="start_date" name="start_date" required="">      <option value="">start date</option>  </select>

instead, if $programdates_items array of array like:

var values = [["january 1, 2018", "april 28, 2018"], ["february 20, 2018", "june 28, 2018", "october 20, 2018"]]; 

the solution can be:

$( "#program_of_interest" ).on('change', function(e) {     $("#start_date").empty();     (values[this.selectedindex - 1] || ['start date']).foreach(function(ele, idx) {         $("#start_date").append($('<option/>', {text: ele, value: ele}));     }) }); 

var values = [["january 1, 2018", "april 28, 2018"], ["february 20, 2018", "june 28, 2018", "october 20, 2018"]];    $( "#program_of_interest" ).on('change', function(e) {      $("#start_date").empty();      (values[this.selectedindex - 1] || ['start date']).foreach(function(ele, idx) {          $("#start_date").append($('<option/>', {text: ele, value: ele}));      })  });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>      <select id="program_of_interest" name="program_of_interest" required="">      <option value="">program of interest</option>      <option value="animation">animation</option>      <option value="gamedesign">game design</option>  </select>  <select id="start_date" name="start_date" required="">      <option value="">start date</option>  </select>

the problem array programs/dates php array, hence can't use in javascript function unless i'm misunderstanding.

in order obtain first object, in php need write:

$programdates_items = json_encode(array('animation' => array(0 => "january 1, 2018", 1 => "april 28, 2018"),            'gamedesign' => array(0=>"february 20, 2018", 1=>"june 28, 2018", 2=>"october 20, 2018"))); 

and in javascript can obtain variable writing:

var values = <?php echo $programdates_items; ?>; 

if need second object, in php need write:

$programdates_items = json_encode(array(0 => array(0 => "january 1, 2018", 1 => "april 28, 2018"),          1 => array(0=>"february 20, 2018", 1=>"june 28, 2018", 2=>"october 20, 2018"))); 

and in javascript:

var values = <?php echo $programdates_items; ?>; 

for details can see json_encode


Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -