我有一些单选按钮,我想在选中时将$_SESSION
设置为单选按钮的值。
这样做的最佳方法是什么。
$_SESSION['radio'] = 1
$_SESSION['radio'] = 2
$_SESSION['radio'] = 3
答案 0 :(得分:0)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js" type="text/javascript"></script>
<form>
Radio 1<input type="radio" name="radio" onClick="set_session('radio1');" >
Radio 2<input type="radio" name="radio" onClick="set_session('radio2');" >
Radio 3<input type="radio" name="radio" onClick="set_session('radio3');" >
</form>
<script>
function set_session(radio)
{
if(radio != '')
{
$.ajax({
type: "POST",
url: 'session.php?radio='+radio,
success: function(response) {
if(response!= null)
{
alert(response+' Session Set');
}
}
});
}
}
</script>
你的session.php文件中的
<?php
if(isset($_POST['radio']) && !empty($_POST['radio']))
{
session_start();
if($_POST['radio'] == 'radio1')
{
$radio1 = $_SESSION[$_POST['radio']];
echo $radio1;
}
else if($_POST['radio'] == 'radio2')
{
$radio2 = $_SESSION[$_POST['radio']];
echo $radio2;
}
else
{
$radio3 = $_SESSION[$_POST['radio']];
echo $radio3;
}
die;
}
?>