PHP单选按钮和会话 [英] PHP Radio buttons and Session

查看:121
本文介绍了PHP单选按钮和会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,
我有两个单选按钮'A'和'B',用户可以选择任何一个,
默认按钮'A'被选中,但当用户来到我的网站并选择B,然后选择更改为按钮B,但问题是,当用户移动到我的网站中的另一个页面时,



< ??
如何解决?
可以在缓存和会话的帮助下完成?如果是,那么如何:



这里是按钮,

  < form name =f1method =POSTaction =<?php echo $ PHP_SELF;?>> 
< span>家庭过滤器:< / span>
< ul>
< li>< input type =radioname =r1value =oonClick =submit <?php echo($ _SESSION ['r1'] ==o)? 'checked =checked':''; ?><?php echo($ _POST ['r1'] =='o')? 'checked =checked':''; ?> />开< / li>
< li>< input type =radioname =r1value =ponClick =submit <?php echo($ _SESSION ['r1'] ==p)? 'checked =checked':''; ?><?php echo($ _POST ['r1'] =='p')? 'checked =checked':''; ?> />关闭< / li>
< / ul>
< / form>



<?php
session_start();

//我不知道在这里写什么才能使它工作:S
?>



当我打开页面时,两个按钮都未被选中,我怎么做,当没有选择按钮'A'

解决方案

您需要将_POST变量转换为_SESSION变量。它起作用,因为你允许POST或SESSION变量来设置检查:

 <?php echo($ _SESSION [ 'r1'] ==o)? 'checked =checked':''; ?><?php echo($ _POST ['r1'] =='o')? 'checked =checked':''; ?> 

但这是不必要的。在您的信息页顶部应该有:

 <?php 
session_start
if(isset($ _ POST ['r1'])){
$ _SESSION ['r1'] = $ _ POST ['r1'];
}
?>

然后向下只需要

 <?php echo($ _SESSION ['r1'] ==o)? 'checked =checked':''; ?> 

额外注意:确保session_start非常开始的页面,它发生在每个页面的顶部,您希望会话变量可用。


Ok, I have two radio buttons 'A' and 'B' and user have a choice to select anyone of them, by default button 'A' is selected but when user come to my website and selects 'B' then selection changes to button 'B' however the problem is,

when the user move to another page in my site the selection goes back to default ?? How to fix it ?? can it be done with the help of cache and session ? if yes then how :?

here is the buttons,

<form name="f1" method="POST" action="<?php echo $PHP_SELF;?>">
<span>Family filter:</span>
<ul>
<li><input type="radio" name="r1" value="o" onClick="submit();" <?php echo ($_SESSION['r1'] == "o") ? 'checked="checked"' : ''; ?><?php echo ($_POST['r1'] == 'o' ) ? 'checked="checked"' : ''; ?> />On</li>
<li><input type="radio" name="r1" value="p" onClick="submit();" <?php echo ($_SESSION['r1'] == "p") ? 'checked="checked"' :''; ?><?php echo ($_POST['r1'] == 'p') ? 'checked="checked"' : ''; ?> />Off</li>
</ul>
</form>



<?php
session_start();

//I don't know what to write here in order to make it work :S ?>

also when i open the page both buttons are unselected how could i do that the button 'A' remain checked by default when none selected

解决方案

You need to translate your _POST variables into _SESSION variables. It works at first because you allow either POST or SESSION variables to set the check:

<?php echo ($_SESSION['r1'] == "o") ? 'checked="checked"' : ''; ?><?php echo ($_POST['r1'] == 'o' ) ? 'checked="checked"' : ''; ?>

But this is unnecessary. At the very top of your page you should have this:

<?php
session_start();
if (isset($_POST['r1'])){
    $_SESSION['r1']=$_POST['r1'];
}
?>

and then down below you only need

<?php echo ($_SESSION['r1'] == "o") ? 'checked="checked"' : ''; ?>

Extra note: Make sure that the session_start() call occurs at the very beginning of the page, and that it occurs at the very top of every page where you want the session variable to be available.

这篇关于PHP单选按钮和会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆