0

我从 PHP 和 HTML 开始,想用三个下拉列表(选择标签)执行简单的代码。但是当我从一个中进行选择时,其他两个选定的值就会消失。我应该在提交时保存值然后回显吗?如果是,我该怎么做。示例代码将不胜感激。

    <?php // formtest_2.php 

$name_vid = $name_type = $name_model = "";

if (isset($_POST['vid']))$name_vid=($_POST['vid']);
else $name_vid="no";

if (isset($_POST['type']))$name_type=sanitizeString($_POST['type']);
else $name_type="no";

if (isset($_POST['model']))$name_model=sanitizeString($_POST['model']);
else $name_model="no";

echo <<<_END
<html>
    <head>
        <title>Form Test Two</title>
    </head>
    <body>

    Make your selection:

<form action="formtest_2.php" method="post">

    <select name="vid" size="1">

    <option value="" selected>Product</option>

    <option value = "apple">apple</option>
    <option value = "cherry">cherry</option>
    <option value = "orange">orange</option>

    </select>

<input type="submit"/><br />        

    <select name="type" size="1">

    <option value="" selected>Color</option>

    <option value = "green">green</option>
    <option value = "red">red</option>
    <option value = "orange">orange</option>

    </select>

<input type="submit"/><br />        

    <select name="model" size="1">

    <option value="" selected>Model</option>

    <option value = "1-a">1-a</option>
    <option value = "2-b">2-b</option>
    <option value = "3-c">3-c</option>

    </select>

<input type="submit"/><br />

    </form>
    </body>
</html>
_END;

echo "Your selection is: $name_vid and $name_type and $name_model"; 

function sanitizeString($var)
{
    $var = stripslashes($var);
    $var = htmlentities($var);
    $var = strip_tags($var);

    return $var;
}
?>
4

2 回答 2

1

我认为您需要在提交后重新选择下拉列表的值。以下代码将是一个修复。

<?php // formtest_2.php 
$name_vid = $name_type = $name_model = "(nothing)";

if (isset($_POST['vid']) && $_POST['vid']) $name_vid=($_POST['vid']);

if (isset($_POST['type']) && $_POST['type']) $name_type=sanitizeString($_POST['type']);

if (isset($_POST['model']) && $_POST['model']) $name_model=sanitizeString($_POST['model']); 
?>
<html>
    <head>
        <title>Form Test Two</title>
    </head>
    <body>

    Make your selection:
    <form action="formtest_2.php" method="post">
        <select name="vid" size="1">    
            <option value="">Product</option>        
            <option value = "apple" <?php if($name_vid == 'apple') echo 'selected="selected"'; ?>>apple</option>
            <option value = "cherry" <?php if($name_vid == 'cherry') echo 'selected="selected"'; ?>>cherry</option>
            <option value = "orange" <?php if($name_vid == 'orange') echo 'selected="selected"'; ?>>orange</option>    
        </select>

        <br />

        <select name="type" size="1">
            <option value="">Color</option>    
            <option value = "green" <?php if($name_type == 'green') echo 'selected="selected"'; ?>>green</option>
            <option value = "red" <?php if($name_type == 'red') echo 'selected="selected"'; ?>>red</option>
            <option value = "orange" <?php if($name_type == 'orange') echo 'selected="selected"'; ?>>orange</option>    
        </select>

        <br />        

        <select name="model" size="1">    
            <option value="">Model</option>    
            <option value = "1-a" <?php if($name_model == '1-a') echo 'selected="selected"'; ?>>1-a</option>
            <option value = "2-b" <?php if($name_model == '2-b') echo 'selected="selected"'; ?>>2-b</option>
            <option value = "3-c" <?php if($name_model == '3-c') echo 'selected="selected"'; ?>>3-c</option>    
        </select>

        <input type="submit"/><br />
    </form>
    </body>
</html>
<?php
echo "Your selection is: $name_vid and $name_type and $name_model"; 

function sanitizeString($var)
{
    $var = stripslashes($var);
    $var = htmlentities($var);
    $var = strip_tags($var);

    return $var;
}
?>

我不会使用heredoc语法 ("<<<") 来打印 HTML。

于 2012-12-26T10:26:13.753 回答
0

试试这个,我做了一个提交按钮,并在提交后重新选择了值:

<?php
// formtest_2.php

$name_vid = $name_type = $name_model = "";

if (isset($_POST['vid']))
    $name_vid = ($_POST['vid']);
else
    $name_vid = "no";

if (isset($_POST['type']))
    $name_type = sanitizeString($_POST['type']);
else
    $name_type = "no";

if (isset($_POST['model']))
    $name_model = sanitizeString($_POST['model']);
else
    $name_model = "no";


///////////////////
if($name_vid == 'apple')
{
    $v1 = 'selected';
}
elseif($name_vid == 'cherry')
{
    $v2 = 'selected';
}
elseif($name_vid == 'orange')
{
    $v3='selected';
}
 ////////////////////////
if($name_type == 'green')
{
    $t1 = 'selected';
}
elseif($name_type == 'red')
{
    $t2 = 'selected';
}
elseif($name_type == 'orange')
{
    $t3='selected';
}


///////////////////////
if($name_model == '1-a')
{
    $m1 = 'selected';
}
elseif($name_model == '2-b')
{
    $m2 = 'selected';
}
elseif($name_model == '3-c')
{
    $m3='selected';
}

?>
<html>
    <head>
        <title>Form Test Two</title>
    </head>
    <body>

        Make your selection:

        <form name="fruite_form" action="formtest_2.php" method="post">

            <select name="vid" size="1">

                <option value="" selected>Product</option>

                <option <?=$v1?> value = "apple">apple</option>
                <option <?=$v2?> value = "cherry">cherry</option>
                <option <?=$v3?> value = "orange">orange</option>

            </select>



            <select name="type" size="1">

                <option value="" selected>Color</option>

                <option <?=$t1?> value = "green">green</option>
                <option <?=$t2?>value = "red">red</option>
                <option <?=$t3?>value = "orange">orange</option>

            </select>



            <select name="model" size="1">

                <option value="" selected>Model</option>

                <option <?=$m1?> value = "1-a">1-a</option>
                <option <?=$m2?>value = "2-b">2-b</option>
                <option <?=$m3?>value = "3-c">3-c</option>

            </select>

            <input name="submitButtom" id="submitButton" type="submit"/><br />

        </form>
    </body>
</html>


<?
echo "Your selection is: $name_vid and $name_type and $name_model";

function sanitizeString($var)
{
    $var = stripslashes($var);
    $var = htmlentities($var);
    $var = strip_tags($var);

    return $var;
}
?>

随意顶部添加您的提交按钮。但是给每个人一个唯一的名字,也不要忘记给你的表单命名,作为一个好的做法,也给一个 ID,它与 name 属性相同。

更新:只是为了确保您确实收到了帖子,因为这是您最初的问题,请在代码末尾打印此内容

echo $_POST['vid'] . ' ' . $_POST['type'] . ' ' . $_POST['model'] . '<br/><br/>';

并查看 post 提交后是否真的持有该值,如果您从 post 中获得值,而不是从分配的变量中获得值,则检查您在顶部使用的条件以分配您的值。

// 您需要检查 post isset 是否有实际值,只有这样,才应该分配变量。就像司徒说的一样。

于 2012-12-26T10:54:43.027 回答