Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reload the page with parameter triggered by JavaScript dropdown change event

I want a form to be reloaded and preselected with the selection made. Here's the HTML form I have:

<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 dropdown box will be on the Select option by default and the textbox (current) will be empty.

When the user selects any option from dropdown the page needs to reload and that dropdown item should be selected and the value of that dropdown should be in the text input field.

What's the JavaScript function that I need to make this happen?

like image 859
Naveed Avatar asked Jan 22 '23 16:01

Naveed


1 Answers

Like this:

<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>
like image 101
Sarfraz Avatar answered Jan 24 '23 06:01

Sarfraz