用于下拉列表的PHP粘性表单

用于下拉列表的PHP粘性表单,php,drop-down-menu,sticky,Php,Drop Down Menu,Sticky,我无法使下拉列表保持粘性(这样,在第一次提交表单后,所选内容将在下一页的表单中预选)。我删除了一些我认为不相关的代码。我试着让价值$_GET[“大陆”],但没有成功。有人有什么想法吗?请参见函数createpulldown <!DOCTYPE html> <head> <title>Homework 14</title> </head> <body> <?php if (isset($_GET['s

我无法使下拉列表保持粘性(这样,在第一次提交表单后,所选内容将在下一页的表单中预选)。我删除了一些我认为不相关的代码。我试着让价值$_GET[“大陆”],但没有成功。有人有什么想法吗?请参见函数createpulldown

 <!DOCTYPE html>
 <head>
 <title>Homework 14</title>  
 </head>
 <body>


<?php 
if (isset($_GET['submitted']))
    handleform($_GET['country']);

displayform("country");

?>
</body>

function displayform($menuname) {
    echo "<fieldset><legend>Select a continent and I will show you information from the CIA about it.</legend>
            <form method = 'get'>";
            createpulldown($menuname);
            echo "<input type='submit' name='submitted' value='Search'>
            </form>
          </fieldset>";
}

function createpulldown($menuname) {
    echo "<select name='$menuname'>";
    $dbc = connectToDB();
    $query = "SELECT Continent FROM countries GROUP BY Continent";
    $result = performQuery($dbc, $query);

    while ($row=mysqli_fetch_array($result, MYSQLI_ASSOC)){

        $continent = $row['Continent'];

        if (isset($_GET[$menuname]))
            echo "<option name='continent' value = $continent selected>$continent</option>\n";
        else
            echo "<option name='continent' value = $continent>$continent</option>\n";
    }

    echo "</select>";
    disconnectFromDB($dbc, $result);
}
?>

作业14
函数displayform($menuname){
echo“选择一个大陆,我会向你展示中央情报局关于它的信息。
";
createpulldown($menuname);
回声“
";
}
函数createpulldown($menuname){
回声“;
$dbc=connectToDB();
$query=“按大陆从国家组中选择大陆”;
$result=performQuery($dbc,$query);
而($row=mysqli\u fetch\u数组($result,mysqli\u ASSOC)){
$Continental=$row['Continental'];
if(isset($\u GET[$menuname]))
回显“$大陆\n”;
其他的
回显“$大陆\n”;
}
回声“;
disconnectFromDB($dbc,$result);
}
?>

您想检查从$\u GET检索到的值是否等于选项值之一

请尝试以下while语句:

while ($row=mysqli_fetch_array($result, MYSQLI_ASSOC)){
    $continent = $row['Continent'];
    if ($_GET[$menuname] == $continent)
        echo "<option name='continent' value='$continent' selected>$continent</option>\n";
    else
        echo "<option name='continent' value='$continent'>$continent</option>\n";
}
while($row=mysqli\u fetch\u数组($result,mysqli\u ASSOC)){
$Continental=$row['Continental'];
如果($\u GET[$menuname]==$constance)
回显“$大陆\n”;
其他的
回显“$大陆\n”;
}
我还修复了一个语法错误。您需要将选项值用单引号括起来。

while($row=mysqli\u fetch\u array($result,mysqli\u ASSOC)){
while ($row=mysqli_fetch_array($result, MYSQLI_ASSOC)){

    $continent = $row['Continent'];

    if (isset($_GET[$menuname]))
        echo "<option name='continent' value = $continent selected>$continent</option>\n";
    else
        echo "<option name='continent' value = $continent>$continent</option>\n";
}
$Continental=$row['Continental']; if(isset($\u GET[$menuname])) 回显“$大陆\n”; 其他的 回显“$大陆\n”; }
如果将名为$MENUAME的GET参数传递给脚本,您将在此处进行检查,但对于您创建的所有选项,这是相同的,要么参数不存在


相反,您要做的是将参数的值与您正在编写的当前值进行比较–如果它们相等,请选择当前选项,如果不相等,请不要选择。

sorry;这样,在第一次提交表单后,所选内容将在下一页的表单中预选。啊,好的。我个人不知道怎么做。我建议你用上面提到的内容编辑你的问题。CheersI将使用HTML5本地存储,这很好地解释了这一点: