html - This form doesn't update the user selection at the drop down list when submitting the form through PHP -
<!doctype html> <html> <script type="text/javascript"> function updateselecttarget () { var id = this.options[this.selectedindex].value; var targets = this.parentnode.getelementsbytagname("select"); var len = targets.length; (var = len - 1; > 0; --i) { if (targets[i].id == id) { targets[i].style.display = "block"; } else { targets[i].style.display = "none"; } } } function initchangehandlers () { var i, el; var allselectelements = document.getelementbyid("myform").getelementsbytagname("select"); (i in allselectelements) { el = allselectelements[i]; if (el.classname == "changeable") { el.onchange = updateselecttarget; el.onchange(); } } } window.onload = initchangehandlers; </script> <head> </head> <body> <form method="post" action="<?php echo $_server['php_self']; ?>" name="myform" id="myform"> <fieldset> <legend>location</legend> <select id="country" class="changeable" name="country"> <option value="england">england</option> <option value="france">france</option> <option value="germany">germany</option> </select> <hr/> <select id="england" name="city"> <option value="birmingham">birmingham</option> <option value="liverpool">liverpool</option> <option value="london">london</option> </select> <select id="france" class="hidden" name="city"> <option value="lyon">lyon</option> <option value="marseille">marseille</option> <option value="paris">paris</option> </select> <select id="germany" class="hidden" name="city"> <option value="berlin">berlin</option> <option value="hamburg">hamburg</option> <option value="munich">munich</option> </select> </fieldset> <input type="submit" name="submit" value="submit"> <?php echo "the selection country $country , city $city"; ?> </form> </body> </html>
to fix code, try setting value on postback:
<select id="country" class="changeable" name="country" id="country"> <option value="england">england</option> <option value="france">france</option> <option value="germany">germany</option> </select> <?php if(isset($_post['country'])){ ?> <script type="text/javascript"> document.getelementbyid('country').value = '<?php echo $_post['country']; ?>'; </script> <?php } ?>
Comments
Post a Comment