我遇到网页上出现单选按钮的问题,有人可以告诉我为什么在用户按下提交后它不会保留其选择吗?
<?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']; ?>
我有另一套低于饮食要求的单选按钮,在提交后它的功能和保持价值很好,所以我不明白为什么饮食要求单选按钮不起作用。
为清晰起见,这是功能单选按钮:
<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>
答案 0 :(得分: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>