本文介绍了提交后保留单选按钮选择-PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的页面上出现单选按钮时出现问题,有人可以告诉我为什么用户按下提交"后它将不保留其选择吗?
I am experiencing an issue with radio buttons present on my page, can anyone tell me why it wont retain its selection after the user has pressed submit?
<?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']; ?>
我还有一套低于饮食要求的单选按钮,提交后功能正常并保持其价值,所以我不明白为什么饮食要求单选按钮无法正常工作.
I have another set of radio buttons below the dietary requirements, which functions and retains its value fine after submit, so I dont understand why the dietary requirement radio buttons arent working.
为清楚起见,以下是功能正常的单选按钮:
Here is the functioning radio buttons for clarity:
<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>
推荐答案
我认为您正在寻找这样的东西:
I think your looking for something like this:
<?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>
这篇关于提交后保留单选按钮选择-PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!