如何在php的组合框中显示所选值?

时间:2016-04-20 09:21:35

标签: php

我已经编写了这段代码......但这无法显示文字......不确定代码是否有问题。我是PHP新手并尝试创建一个获取客户详细信息并将其放入sql的页面。

<!DOCTYPE html>
<html>
<body>

<?php
$initial="";
?>

<form method="post"
    action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>">

Intials: <select id="cmbInitial" name="initial" onchange="showUser(this.value)">
    <option value="0">Select initial</option>
    <option value="1">Mr.</option>
    <option value="2">Mrs.</option>
    <option value="3">Ms.</option>
    <option value="4">M/s</option>
</select> <br>
<br>

<input type="submit" name="submit" value="Submit"></form>
<br>
<br>

<?php
echo "Customer Intial: $initial <br>";
?>

</body>
</html>

3 个答案:

答案 0 :(得分:0)

试试这个:

$initial="";
if(isset($_POST['initial'])){
    $initial=htmlentities($_POST['initial']);
}

您正确发送了POST,但是您没有将POST中给出的值放入$initial变量。

答案 1 :(得分:0)

<!DOCTYPE html>
<html>
    <body>

        <?php
        $initial = "";
        ?>

        <form method="post"
              action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>">

            Intials: <select id="cmbInitial" name="initial" onchange="showUser(this.value)">
                <option value="0">Select initial</option>
                <option value="1">Mr.</option>
                <option value="2">Mrs.</option>
                <option value="3">Ms.</option>
                <option value="4">M/s</option>
            </select> <br>
            <br>

            <input type="submit" name="submit" value="Submit"></form>
        <br>
        <br>

        <?php
        $initial = filter_input(INPUT_POST, "initial");//GET the input in post method
        if ($initial == 1) {
            $initial_value = "Mr.";
        } elseif ($initial == 2) {
            $initial_value = "Mrs.";
        } elseif ($initial == 3) {
            $initial_value = "Ms.";
        } elseif ($initial == 4) {
            $initial_value = "M/s";
        } else {
            $initial_value = "Select initial";
        }
        echo "Customer Intial: $initial_value <br>";
        ?>

    </body>
</html>

答案 2 :(得分:0)

尝试,               

<form method="post" action=<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>>

    Intials: <select id="cmbInitial" name="initial" onchange="showUser(this.value)">
        <option value="0">Select initial</option>
        <option value="1">Mr.</option>
        <option value="2">Mrs.</option>
        <option value="3">Ms.</option>
        <option value="4">M/s</option>
    </select> <br>
    <br>

    <input type="submit" name="submit" value="Submit">
</form>
<br>
<br>

<?php

if(isset($_POST['initial'])){
    $initial=$_POST['initial'];
} else {

    $initial = "empty";
}

echo "Customer Intial : ".$initial;
?>

</body>
</html>