Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

251
Views
How to keep multiple selected values from dropdown after form submission

I am trying to keep the values selected after form submission, when the user selects more than one value is should still be selected after the submit button has been clicked.

<?php
$example = $_POST["name1"];
?>   

<form action="" method="post">
<select id="id1" name="name1" class="selectpicker" title="Select Currencies" multiple="multiple">
<option <?php if (isset($example) && $example=="a") echo "selected";?>>A</option>
<option <?php if (isset($example) && $example=="b") echo "selected";?>>B</option>
<option <?php if (isset($example) && $example=="c") echo "selected";?>>C</option>
</select>

<button name="submit" type="submit" value="Submit">SUBMIT</button>
</form>



<script>

$('#id1').on('change', function() {

var values = $(this).val()
$("#id2 option").hide() //hide all options
$('#id2').selectpicker('deselectAll') //if want to remove all selcted optn

if (values.length > 0) {
for (var i = 0; i < values.length; i++) {
  //show where value is same..
  $("#id2 option[value=" + values[i] + "]").show()
}
} else {
$("#id2 option").show() //show all options 
}
$('#id2').selectpicker('refresh'); //refresh selctpicker

});


</script>
about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Use pure JS

var options = document.getElementById('id1').options;
for (let i = 0; i < options.length; i++) { 
  console.log(options[i].value);//log the value
}
OR use jquery

$("#id1 option").each(function(){
    var thisOptionValue=$(this).val();
});

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!