Reload page with parameter caused by change event of JavaScript dropdown menu

I want the form to be reloaded and preselected with the selected selection. Here I have an HTML form:

<form name='vehicleform'>

<select name='vehiclelist'>
  <option value=''>Select</option>
  <option value='bus'>Bus</option>
  <option value='bike'>Bike</option>
  <option value='car'>Car</option>
</select>

<input type='text' name='current'></input>

</form>

When the user opens the page for the first time, the drop-down field will be Select by default , and the text field (current) will be empty.

When the user selects any option from the drop-down list, the page should be reloaded, and this drop-down element should be selected, and the value of this drop-down list should be in the text input field.

What JavaScript function do I need to make this happen?

+3
source share
2

:

<select name='soemname' id="myid" onchange="document.location.href = 'page.php?var=' + this.value">
  <option value=''>Select</option>
  <option value='bus'>Bus</option>
  <option value='bike'>Bike</option>
  <option value='car'>Car</option>
</select>

<script type="text/javascript">
  document.getElementById('myid').value = "<?php echo $_GET['var'];?>";
</script>
+5

sAc answer , onchange location.href. , JS. url action select onchange. , JavaScript , :

<?php $sel = isset($_GET['vehiclelist']) ? $_GET['vehiclelist'] : ""; ?>
<form name='vehicleform' action="thispage.php">

<select name='vehiclelist' onchange="this.parentNode.submit();">
  <option value='' >Select</option>
  <option value='bus' <?php echo $sel == "bus" ? "selected" : "";?>>Bus</option>
  <option value='bike' <?php echo $sel == "bike" ? "selected" : "";?>>Bike</option>
  <option value='car' <?php echo $sel == "car" ? "selected" : "";?>>Car</option>
</select>

<input type='text' name='current' value="<?php echo htmlspecialchars($_GET['var']); ?>"></input>

</form>

- , JavaScript.

+3

Source: https://habr.com/ru/post/1756905/


All Articles