-1

I am trying to make a dropdown menu. Where the selected item show the content from that specific option. How do I do that?

<select name="season" id="season">
    <option selected="selected">Kies Seizoen</option>
    <?php
    $Seasons  = WaterpoloAPICached::call("Seasons", "getSeasons");
    $SeasonId = "";
    foreach ($Seasons as $Season)
    { ?>
        <option value="<?php echo $SeasonId = $Season->Id ?>"><?php echo $Season->DateFrom ?> - <?php echo $Season->DateTo ?></option>
        <?php
    } ?>
</select>
<?php
$test = (isset($_POST['season'])) ? $_POST['season'] : '';

?>
<h2>
    <strong>Seizoen <?php echo $Season->DateFrom ?> - <?php echo $Season->DateTo ?></strong>
</h2>

Thank you in advance!

Ronnie Oosting
  • 1,252
  • 2
  • 14
  • 35
  • Possible duplicate of [Change the selected value of a drop-down list with jQuery](https://stackoverflow.com/questions/499405/change-the-selected-value-of-a-drop-down-list-with-jquery) – Zain Farooq Aug 14 '18 at 14:00

1 Answers1

0

I replaced your $Seasons object with an array of objects, so the syntax is identical. Is this what you are looking for?

<script
  src="https://code.jquery.com/jquery-3.3.1.slim.min.js"
  integrity="sha256-3edrmyuQ0w65f8gfBsqowzjJe2iM6n0nKciPUp8y+7E="
  crossorigin="anonymous"></script>
<script>
  $(function(){
    $("select#season").change(function(){ //whenever someone changes the select list with id of 'season'
      //I set the span within a strong within an h2, to the currently selected option's text value.
    $("h2 strong span").text($(this).find("option:selected").text());
    })
  })
</script>
  <select name="season" id="season">
  <option selected="selected">Kies Seizoen</option>
  <?php
    $Seasons    =  array(
      (object)array('Id' => 1, 'DateFrom' => '2018-01-01', 'DateTo' => '2018-3-31'  ),
      (object)array('Id' => 2, 'DateFrom' => '2018-04-01', 'DateTo' => '2018-06-30' ),
      (object)array('Id' => 3, 'DateFrom' => '2018-07-01', 'DateTo' => '2018-09-30' ),
      (object)array('Id' => 4, 'DateFrom' => '2018-10-01', 'DateTo' => '2018-12-31' )
    );
    $SeasonId   = "";
    foreach($Seasons as $Season) {
  ?>
      <option value="<?php echo $SeasonId = $Season->Id ?>"><?php echo $Season->DateFrom ?> - <?php echo $Season->DateTo ?></option>
  <?php
    }  //foreach($Seasons as $Season) {
  ?>
</select>
  <?php
  $test = (isset($_POST['season'])) ? $_POST['season'] : '';

  ?>
  <h2><strong>Seizoen <span><?php echo $Season->DateFrom ?> - <?php echo $Season->DateTo ?></span></strong><h2>
pendo
  • 792
  • 2
  • 5
  • 27