0

I have a dropdown like this:

<form action="" id="myForm" method="post">
<select id="choose">
<option value="test1">Test1</option>
<option value="test2">Test2</option>
<option value="test3">Test3</option>
</select>

I use this script to call another page, and load the data in div UPDATE:

<div id="update"></div>

 <script type="text/javascript">
  $(document).ready(function () {
    var datum = $('#choose option:selected').text();
    $("#choose").on("change", function () {
          $("#update").load("boekingstap0_inlay.php?zipcodeFrom=<?php echo $_POST['zipcodeFrom']; ?>&date="+datum);

    });
});

When I change the dropdown, the returned value stays "test1". The page does reload, I put an Alert message on the page to be loaded, which echoës the value $_GET['date'].

What am I doin' wrong?

  • 1
    `datum` is a variable that holds a string. Strings are immutable. Just because something else somewhere changes doesn’t mean that the _immutable_ string magically changes with it. That’s not how JavaScript works, thankfully. The `$('#choose option:selected').text()` call needs to be inside the `change` listener. – Sebastian Simon Dec 30 '22 at 16:27
  • Try this $(document).ready(function () { $("#choose").on("change", function () { var datum = $('#choose option:selected').text(); $("#update").load("boekingstap0_inlay.php?zipcodeFrom=&date="+datum); }); }); – Yassir Irfan Jan 02 '23 at 11:10
  • Your `` is unsafe. Please use proper encoding. See [How do I pass variables and data from PHP to JavaScript?](/q/23740548/4642212). – Sebastian Simon Jan 02 '23 at 13:46

0 Answers0