views:

44

answers:

2

I have a form in vehicles.php:

<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 user open vehicle.php first time then in dropdown, 'Select' option will be selected and textbox(current) will be empty. When user select any option from dropdown then page reload and that dropdown item should be selected and value of that dropdown should be in textbox(current).

I need a simple js function and not a complex method. Any idea?

Thanks

+2  A: 

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>
Sarfraz
+2  A: 

sAc's answer tells you how you can use the onchange event to modify the location.href property. This will work, but it's not accessible to users with JS disabled. I would set a url in the form's action attribute and have the select element submit the form in the onchange event. This way, users with JavaScript can still press the return key to reload the page with the same effect:

<?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>

The end result is a feature that is accessible to users with JavaScript disabled too.

Andy E
@Andy E: I knew but i was too lazy to write php code :(
Sarfraz
@sAc: ha, I can relate to that :)
Andy E
Actually, I have to submit this form separately to store textbox(current) value in database using a submit button(missed in question). Therefore I have to call another script on submit of this form to store textbox value in database. I think js is only option in this case?
NAVEED
@NAVEED: you can specify multiple submit buttons for a form and, if the *name* attribute is provided with them you can detect which button was used to send the form. If you have a server-side condition to catch when the "real" submit is sent and record the data in the database. See also http://articles.techrepublic.com.com/5100-10878_11-5242116.html.
Andy E
@Andy E: It is nice information to use in future. Thanks
NAVEED
HTML injection vulnerability in the input value. Remember `htmlspecialchars()`.
bobince
@bobince: right you are. This sort of stuff doesn't always occur to me when I'm writing answers here, thanks for the nudge.
Andy E