PHP如何保持单选按钮状态到下一页 [英] PHP How to keep radio button state to the next page
问题描述
我用单选按钮制作了一个表单。如何在用户选择一个选项后保留它的状态?然后在下一页中再次显示相同的表单,并启用用户选择的单选按钮。
// page1.html
< form method =postaction =page2.html>
< p>
< input type =radioname =q1value =A/>
A.< br />
< input type =radioname =q1value =B/>
B.< br />
< input type =radioname =q1value =C/>
C.< br />
< input type =radioname =q1value =D/>
D.
< p>
< input type =submitname =actionvalue =Enter/>
< / p>
< / form>
要获取 q1
在下一页中,您可以使用 $ _ POST ['q1']
。您可以验证该元素是否已发布,并且该值通过使用 if(isset($ _ POST ['q1']))&&& $ _POST ['q1'] == VALUE
。所以你的表单代码看起来像 -
pre $ lt; code>< input type =radioname =q1value =A <?php if(isset($ _ POST ['q1'])&&($ _POST ['q1'] =='A'))echo'checked =checked';?> />
A.< br />
< input type =radioname =q1value =B<?php if(isset($ _ POST ['q1'])&&($ _POST ['q1']] =='B'))echo'checked =checked';?> />
B.< br />
< input type =radioname =q1value =C<?php if(isset($ _ POST ['q1'])&&($ _POST ['q1']] =='C'))echo'checked =checked';?> />
C.< br />
< input type =radioname =q1value =D<?php if(isset($ _ POST ['q1'])&&($ _POST ['q1']] =='D'))echo'checked =checked';?> />
I made a form with radio buttons. How can I preserve it's state after a user picked a choice? Then same form will show again in the next page and the radio button that the user picked is enabled.
//page1.html
<form method="post" action="page2.html">
<p>
<input type="radio" name="q1" value="A" />
A. <br />
<input type="radio" name="q1" value="B" />
B. <br />
<input type="radio" name="q1" value="C" />
C. <br />
<input type="radio" name="q1" value="D" />
D.
<p>
<input type="submit" name="action" value="Enter" />
</p>
</form>
To get the value of q1
on the next page, you would use $_POST['q1']
. You can verify that the element has been posted, and the value matches the specific radio button by using if(isset($_POST['q1'])) && $_POST['q1'] == VALUE
. So your form code would look like -
<input type="radio" name="q1" value="A" <?php if(isset($_POST['q1']) && ($_POST['q1'] == 'A')) echo 'checked="checked" ';?>/>
A. <br />
<input type="radio" name="q1" value="B" <?php if(isset($_POST['q1']) && ($_POST['q1'] == 'B')) echo 'checked="checked" ';?>/>
B. <br />
<input type="radio" name="q1" value="C" <?php if(isset($_POST['q1']) && ($_POST['q1'] == 'C')) echo 'checked="checked" ';?>/>
C. <br />
<input type="radio" name="q1" value="D" <?php if(isset($_POST['q1']) && ($_POST['q1'] == 'D')) echo 'checked="checked" ';?>/>
这篇关于PHP如何保持单选按钮状态到下一页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!