保留单选按钮选择提交后 - PHP(Retain radio button selection a

2019-10-21 07:39发布

我遇到了现在我的网页上单选按钮的一个问题,谁能告诉我,为什么它不会保留其选择的用户按下后提交?

<?php
session_start();
if(isset($_POST['submit']))
if(!empty($_POST['diet']))
?>

...

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

<strong>Dietary Requirements:</strong>
<br><br>
Vegetarian <input type="radio" name="diet" <?php if (isset($_POST['diet']) && $_POST['diet']=="Vegetarian") echo "checked";?> value="Vegetarian">
<br><br>
Vegan <input type="radio" name="diet" <?php if (isset($_POST['diet']) && $_POST['diet']=="Vegan") echo "checked";?> value="Vegan">
<br><br><br>

...

<input type="submit" id="submit" name="submit" value="Submit">

确认页:

<b>Dietary Requirements: </b>
<br><br><?php if(isset($_SESSION['diet'])) echo $_SESSION['diet']; ?>

我有另一组下面的饮食要求单选按钮,它的功能并保留其价值罚款后提交,所以我不明白为什么膳食需求单选按钮的arent工作。

下面是清晰的功能单选按钮:

<strong> VIP area* : </strong> <br><br>
Yes (+£5) <input type="radio" name="vip" <?php if (isset($vip) && $vip=="Yes") echo "checked";?> value="Yes">
<br><span id="vip" class="error"><?php echo $vipErr;?></span><br>
No <input type="radio" name="vip" <?php if (isset($vip) && $vip=="No") echo "checked";?> value="No">
<br><br><br>

Answer 1:

我认为你在寻找这样的事情:

<?php
    session_start();

    if(isset($_POST['submit'])) {
        if(!empty($_POST['diet']))
            $_SESSION['diet'] = $_POST['diet'];
    }

    if(isset($_SESSION['diet']))
        echo $_SESSION['diet']
?>


<form action="" method="post">
    <strong>Dietary Requirements:</strong>
    <br><br>
    Vegetarian <input type="radio" name="diet" <?php if (isset($_POST['diet']) && $_POST['diet']=="Vegetarian") echo "checked";?> value="Vegetarian">
    <br><br>
    Vegan <input type="radio" name="diet" <?php if (isset($_POST['diet']) && $_POST['diet']=="Vegan") echo "checked";?> value="Vegan">
    <br><br><br>
    <input type="submit" name="submit">
</form>


文章来源: Retain radio button selection after submit - PHP