【问题标题】:How session works in codeigniter with ajax会话如何在带有 ajax 的 codeigniter 中工作
【发布时间】:2026-01-18 09:10:02
【问题描述】:

会话在我的代码中不起作用。它在页面刷新时起作用。我有一个名为ravi 的控制器,视图名称是myview。代码是:

<?php    
    class Ravi extends CI_Controller {
        public function __construct() {
            parent::__construct();
            $this->load->library('session');
            $this->load->helper('form');
            $this->load->helper('url');
            $this->load->helper('html');
            $this->load->database();
            $this->load->library('form_validation');
            $this->load->library('encrypt');
            $this->load->helper(array('form', 'url'));
            //load the login model
            // $this->load->model('login_model');
        }

        function index() {
            $this->session->set_flashdata("f1", "<b>Welcome..!!</b>");
            $this->load->view('myview.php');
        }

        function get_registration() {
            $this->session->set_flashdata("f2", "<b>Welcome You have Successsfully Registered..!!</b>");
        }

        function get_login() {
            $this->session->set_flashdata("f3", "<b>Welcome You have Successsfully Logged In..!!</b>");
        }

        function view() {
            $this->session->set_flashdata("f4", "<b>Welcome You Can view..!!</b>");
        }

        function Logout() {
            $this->session->set_flashdata("ravi", "<b>Welcome You have Successsfully Registered Logged Out..!!</b>");
        }

    }
?>

我的观点是:

<html>
<head>
    <title></title>
    <script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
    <script>
        function Registration() {
            $.ajax({
                url: "<?php echo base_url() . 'index.php/ravi/get_registration' ?>",
                type: 'POST', //the way you want to send data to your URL
                success: function (data) {
                    if (data) {
                        $("div#view").html(data);
                    }
                }
            });
        }

        function View() {
            $.ajax({
                url: "<?php echo base_url() . 'index.php/ravi/view' ?>",
                type: 'POST', //the way you want to send data to your URL
                success: function (data) {
                    if (data) {
                        $("div#view").html(data);
                    }
                }
            });
        }

        function Login() {
            $.ajax({
                url: "<?php echo base_url() . 'index.php/ravi/get_login' ?>",
                type: 'POST', //the way you want to send data to your URL
                success: function (data) {
                    if (data)  {
                        $("div#view").html(data);
                    }
                }
            });
        }

        function Logout() {
            $.ajax({
                url: "<?php echo base_url() . 'index.php/ravi/Logout' ?>",
                type: 'POST', //the way you want to send data to your URL
                success: function (data) {
                    if (data) {
                        $("div#view").html(data);
                    }
                }
            });
        }
    </script>
</head>
<body style="background-color: #99BC99">
    <div id="mydiv" align="center">
        <a href="javascript:ravi();"><b>Home</b></a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <a href="javascript:Registration();"><b>Registration</b></a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <a href="javascript:View();"><b>View Users</b></a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <a href="javascript:Login();"><b>Login</b></a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <a href="javascript:Logout();"><b>Logout</b></a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    </div>
    <div id="view">
        <?php
            if ($this->session->flashdata('ravi')) {
                echo $this->session->flashdata('ravi');
            }
            if ($this->session->flashdata('f1')) {
                echo $this->session->flashdata('f1');
            }
            if ($this->session->flashdata('f2')) {
                echo $this->session->flashdata('f2');
            }
            if ($this->session->flashdata('f3')) {
                echo $this->session->flashdata('f3');
            }
            if ($this->session->flashdata('f4')) {
                echo $this->session->flashdata('f4');
            }
        ?>
    </div>
</body>
</html>

【问题讨论】:

  • 你能解释你的问题而不是只提供代码吗?
  • 当我调用 ajax 意味着点击链接会话messege 必须显示,但它没有。刷新后就可以了
  • 你在构造函数中看到足够的代码了吗?
  • 是的,但是构造函数怎么做

标签: php jquery html ajax codeigniter


【解决方案1】:

只有在设置了会话之后,视图才能实现会话。您无需刷新页面即可设置会话。

当我调用 ajax 意味着点击链接会话信息必须是 显示但它没有。刷新后就可以了

您的服务器知道会话已设置,但您的旧渲染 html 不知道。

小黑客:

如果你想不刷新,那么你应该在会话设置后刷新 div 内容

即,

您应该将 div 内的内容替换为新值。在 ajax 成功时,您应该显示分配给会话的值。

注意:

我希望您尝试按照我解释的方式进行操作,但您在这里错过了

success: function (data) {
if (data)
{
   $("div#view").html(data);
}
}

您只能通过具有唯一 id 的单独 div 将旧值替换为新值。

有点像

$("#YourExclusiveDiv").html(data);

【讨论】: