如何在php的组合框中显示选中的值?[重复]

pgx2nnw8  于 2023-04-19  发布在  PHP
关注(0)|答案(3)|浏览(92)

此问题已在此处有答案

PHP get dropdown value and text(4个答案)
3天前关闭。
我写了这段代码...但这无法显示文本...不知道代码有什么问题。我是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>
9q78igpj

9q78igpj1#

试试这个:

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

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

nlejzf6q

nlejzf6q2#

<!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>
67up9zun

67up9zun3#

试试

<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>

相关问题