【问题标题】:Set Session on checked box php Joomla在复选框 php Joomla 上设置会话
【发布时间】:2014-07-09 22:51:39
【问题描述】:

我需要像 session 那样存储某种值,$_Session['ss']=1 然后检查是否设置了会话...但是必须使用 javascript 或 jquery 设置会话,我知道如何在 PHP 中设置会话变量,但我不在 java 中,现在我使用 Java 或 jQuery 的原因是因为我需要在用户选中该框后立即刷新页面..

我试图使用这个例子https://stackoverflow.com/a/20948139/2293454,但我很笨……
所以这就是我想要做的:

用户复选框 -> 脚本设置将在另一个文件中使用的变量会话
之后页面刷新并且用户现在拥有会话变量并且复选框必须保持选中状态,现在用户取消选中会话变量的框并且页面再次刷新......
现在我该怎么做?

感谢您抽出宝贵时间。

【问题讨论】:

  • 您不能使用纯 JavaScript 设置会话,因为它们是在服务器端完成的(使用 PHP)。但是,您可以编写一个 Ajax 请求来获取会话并在使用 Javascript 或 jQuery 后处理它
  • 就在下面,您将找到一个如何使用 ajax 实现此目的的示例。这是一个非常通用的示例,但功能齐全。

标签: javascript php jquery session joomla


【解决方案1】:

您可以使用 jquery/ajax 创建会话...

一个带有复选框的简单 html 表单:

<form>
    <input type="checkbox" id="checkBox" name="my_check_box" value="1" />Check me!
</form>

JS:

$("#checkBox").change(function () {
    if ($(this).is(':checked')) { //we want the rest to run only if checkbox is checked
        var checkValue = $(this).val();
        //alert(checkValue); //uncomment to check the behaviour of the condition ...or delete it!
        var setSess = $.ajax({
            type: "POST",
            url: "create_session.ajax.php",
            data: "valueR=" + checkValue
        });
        setSess.done(function(getResp) {
            var clrResp = $.trim(getResp);
            alert(clrResp); //here we alert the session as it has been "echoed" in the php file
            return true;
        });
    }

});

最后,由 ajax 调用的新文件“create_session.ajax.php”可能看起来像这样:

<?php
session_start();
$value = $_POST["valueR"];

$_SESSION['ss'] = 1; //use this line or the next one
//$_SESSION['ss'] = $value; //commented out because we use the line above that provides a static "1" value... if you want to use the actual "value" of the checkbox use this line instead the one above.
$response = $_SESSION['ss'];
echo $response;

?>

使用 jquery 的非常简单的解决方案...如果您有任何问题或需要更多信息,我很乐意为您提供帮助。

可以在此处找到不带 php 文件的 jquery 功能演示:http://jsfiddle.net/S5dnP/3/

T.

【讨论】:

  • 6 年前我是个哑巴!我无法完成这个简单的任务,现在我正在构建一个完整的 CRM 系统,使用 Angular、Symfony、GO 中的套接字,哇!对我来说已经走了很长一段路,仍然在学习每天的不同是我们不再在堪萨斯了......
猜你喜欢
  • 1970-01-01
  • 2012-05-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-23
相关资源
最近更新 更多