如何存储和调用会话变量中最后单击的单选按钮

发布于 2024-11-08 17:33:44 字数 784 浏览 0 评论 0原文

我试图找出根据用户单击的单选按钮显示文本的最佳方式。我在使用会话来执行此操作时遇到问题。当我使用以下代码时,仅显示与第一次单击的单选按钮相关的信息(例如,如果有人单击单选按钮“A”,然后改变主意并单击单选按钮“B”,则会话似乎认为“A”仍然被点击。

有什么建议吗?

这是 HTML:

<div class="radio">
  <label for="choice1">Choice 1</label>
  <input class="selection" id="choice1" type="radio" name="selection"  value="choice1"/>

<div class="radio">
  <label for="choice2">Choice 2</label>
  <input class="selection" id="choice2" type="radio" name="selection"  value="choice2" />
</div>

这是 PHP 代码:

if (!isset($_SESSION)) {
    session_start();
$_SESSION['formStarted'] = true;
//...

if(($_SESSION['selection']) == 'choice1'){echo 'Text to be included only if choice 1 was selected.';}

I'm trying to figure out the best way to display text based on which radio button a user has clicked. I am having trouble using sessions to do this. When I use the following code, only the information related to the radio button that was first clicked is displayed (e.g., if someone clicks on radion button "A," then changes his mind and clicks on radio button "B," the session seems to think "A" is still clicked.

Any suggestions?

Here's the HTML:

<div class="radio">
  <label for="choice1">Choice 1</label>
  <input class="selection" id="choice1" type="radio" name="selection"  value="choice1"/>

<div class="radio">
  <label for="choice2">Choice 2</label>
  <input class="selection" id="choice2" type="radio" name="selection"  value="choice2" />
</div>

Here's the PHP code:

if (!isset($_SESSION)) {
    session_start();
$_SESSION['formStarted'] = true;
//...

if(($_SESSION['selection']) == 'choice1'){echo 'Text to be included only if choice 1 was selected.';}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

千笙结 2024-11-15 17:33:44
$_SESSION['selection'] = isset($_POST[selection]) ? $_POST[selection] : 'choice1';

或 $_GET 或 $_REQUEST

$_SESSION['selection'] = isset($_POST[selection]) ? $_POST[selection] : 'choice1';

OR $_GET or $_REQUEST

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文