2015-06-18 58 views
0

會話在我的代碼中不起作用。它在頁面刷新時起作用。我有一個名爲ravi的控制器,視圖名稱爲myview。該代碼是:會話如何在ajax codeigniter中工作

<?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> 
+0

東西你能解釋一下你的問題,而不是隻提供的代碼? –

+0

當我打電話給阿賈克斯意味着點擊鏈接會話必須顯示郵件,但它沒有。它在刷新後工作 –

+1

您是否在構造函數中看到足夠的代碼? – Tpojka

回答

0

會話可以通過會話設置後,才視圖來實現。您無需刷新頁面即可設置會話。

當我打電話給阿賈克斯意味着點擊鏈接會話messege必須是 顯示,但它沒有。它刷新

你的服務器知道會話設置,但舊的渲染HTML不知道它的工作原理之後。

微型哈克:

如果你想這樣做不刷新則會話設置

即之後你要刷新的div內容,

此時應更換裏面的內容與新鮮價值的div。在ajax成功中,您應該顯示分配給會話的值。

注:

我希望你努力做到這一點同我解釋,但你在這裏失蹤

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

您只能通過具有獨立的div取代由新的舊值唯一身份。

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