开发者

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;
                for (var i = len - 1; i > 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");
                for (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'];开发者_Python百科 ?>" 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 is $country and the city is $city"; 
    ?> 

</form>

</body>
</html>


To fix your code, try setting the 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 } ?>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜