简体   繁体   中英

How to get content from a drop down menu to text field?

I am a new person in this PHP and Javascript. I have a drop down menu as follows. Want to get the content or value to a text field and retain the value after the page refreshs? How will do this?

<select name="animal" style="width: 350px;">
  <option value="">Please Select</option>
  <option value="Dog">Dog</option>
  <option value="Cat">Cat</option>
  <option value="Cow">Cow</option>
  <option  value="Rat">Rat</option>
</select>

You can save it in the url, than refresh the page with the new url, and take the value from url to be the selected value in the drop down list.

Ex.

window.location.href = window.location.href + '/' + $('select').val();
$('input').text() = window.location.href.split('/').last();

Try this:

<?php
$options = array("Dog", "Cat", "Cow", "Rat");
?>

<select name="animal" style="width: 350px;">
  <option value="">Please Select</option>
<?php
  foreach($options as $option) {
    $selected = $POST['animal'] == $option ? 'selected' : ''; 
    echo '<option $selected value="$option">$option</option>';
  }
?>
</select>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM