Organizational Research By

Surprising Reserch Topic

keep values selected after form submission


keep values selected after form submission  using -'php,javascript,html,wordpress,forms'

<form method="get" action="">
   <select name="name">
      <option value="a">a</option>
      <option value="b">b</option>
   </select>
   <select name="location">
      <option value="x">x</option>
      <option value="y">y</option>
   </select>
   <input type="submit" value="Submit" class="submit" />
</form>


On submitting the form, how do I make sure that the selected values remain selected in the dropdowns? This form is inside wordpress (PHP).
    
asked Sep 30, 2015 by tushar2k6
0 votes
9 views



Related Hot Questions



Government Jobs Opening

answered Sep 30, 2015 by deepak07.s
0 votes

answered Sep 30, 2015 by devkumargupta
0 votes

Js only solution:

var tmpParams = decodeURIComponent(window.location.search.substr(1)).split("&");
for (var i = 0; i < tmpParams.length; i++) {
    var tmparr = tmpParams[i].split("=");
    var tmp = document.getElementsByName(tmparr[0])[0];
    if (!!tmp){
        document.getElementsByName(tmparr[0])[0].value = tmparr[1];
    }
}

or if you are using jquery you can replace

 var tmp = document.getElementsByName(tmparr[0])[0];
 if (!!tmp){
     document.getElementsByName(tmparr[0])[0].value = tmparr[1];
 }

with:

 $('*[name="'+tmparr[0]+'"]').val(tmparr[1]);
answered Sep 30, 2015 by virendra.bajaj
0 votes

Since wordpress already uses jquery you can try something like this:

var POST=;
for(k in POST){
  $("#"+k).val(POST[k]);
}
answered Sep 30, 2015 by sumit_jaiswalmca
0 votes

After trying al this "solves" nothing work. Did some research on w3school before and remember there was explanation of keeping values about radio. But it also works for Select option. See here an example. Just try it out and play with it.


        



answered Sep 30, 2015 by abhimca2006
0 votes

 
 
   

answered Sep 30, 2015 by yogeshplv
0 votes

You can use wordpress function that is selected


  
 
 

answered Sep 30, 2015 by r3tt
0 votes

This works for me!



answered Sep 30, 2015 by kinnari

...