如何将会话数据放入Codeigniter中的文本框中

时间:2017-02-03 08:04:46

标签: codeigniter session

我是Codeigniter的新手。我将文本框值放到会话中并转到下一页,当我单击第二页上的后退按钮时,第一页数据应该仍然在文本框中。我怎样才能做到这一点?请帮帮我。

控制器代码:

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Welcome extends CI_Controller {


public function index(){
    $this->load->view('welcome_message');
}
public function page1(){

    $this->session->set_userdata('page1',$this->input->get());
    $this->config->load('config');
    $this->config->set_item('sess_expiration', '60');
    $this->load->view('pg2');
}
public function page2(){

    $this->session->set_userdata('page2',$this->input->get());
    $this->config->load('config');
    $this->config->set_item('sess_expiration', '60');
    $this->load->view('pg3');
}
public function page3(){
    $this->config->load('config');
    $this->config->set_item('sess_expiration', '60');
    $sess_data=$this->session->userdata();
    $ses_key = key($sess_data);
    if (empty($this->session->userdata("page1")))
    {   
        echo "Session has been Expired~!";
        $this->session->unset_userdata('page1');
        $this->session->unset_userdata('page2');
        //redirect(site_url(),'refresh');
    }
    else
    {
     $q1 = ($sess_data['page1']);
     $q2 = ($sess_data['page2']);

     $result1 = '';
     $result2 = '';
     $result3 = '';
     $result4 = '';

     $result5 = '';
     $result6 = '';
     $result7 = '';
     $result8 = '';

    for ($i = 0; $i <  count($q1); $i++)
    {
        $key=key($q1);
        $val=$q1[$key];
        if ($val<> ' ')
        {
            if ($key === 'input1')
            {
                $result1 = $val;
                //echo $result1;
            }
            else if ($key === 'input2')
            {
                $result2 = $val;
                //echo $result2;
            }
            else if ($key === 'input3')
            {
                $result3 = $val;
                //echo $result3;
            }
            else
            {
                $result4 = $val;
                //echo $result4;
            }

           echo $key ." = ".  $val ." <br> ";
        }
        next($q1);
    }


    for ($i = 0; $i <  count($q2); $i++)
    {
        $key=key($q2);
        $val=$q2[$key];
        if ($val<> ' ')
        {
            if ($key === 'input5')
            {
                $result5 = $val;
                //echo $result5;
            }
            else if ($key === 'input6')
            {
                $result6 = $val;
                //echo $result6;
            }
            else if ($key === 'input7')
            {
                $result7 = $val;
                //echo $result7;
            }
            else
            {
                $result8 = $val;
                //echo $result8;
            }

           echo " <br> <br> " . $key ." = ".  $val ." <br> ";
        }
        next($q2);
    }

    //$this->testing_model->add_data('sess_table',['val'=>$result1,'val2'=>$result2]);
    $this->testing_model->add_data('user_table',['id'=>"default", 'text2'=>$result1, 'text3'=>$result2, 'text4'=>$result3, 'text5'=>$result4]);
    $this->testing_model->add_data('sess_table',['id'=>"default", 'val'=>$result5, 'val2'=>$result6, 'val3'=>$result7, 'val4'=>$result8]);

        echo "Successfully Saved to Database!";
        $this->session->unset_userdata('page1');
        $this->session->unset_userdata('page2');


    //$querydata=$this->testing_model->query('select * from sess_table');
    //print "<pre/>";
    // print_r($querydata);
    // foreach ($querydata as $key => $value) {
    //  print_r(json_decode($value->val,true));
    //  print_r(json_decode($value->val2,true));
    //  // echo json_decode($value->val.' ---- '.$value->val2;
    // }
    }
  }
}

查看1:

<!DOCTYPE html>
<html lang="en">

</head>
<body>
page 1
<form method="get" action="<?= base_url('welcome/page1') ?>">
    input 1 <input name="input1" type="text"/> <br/>
    input 2 <input name="input2" type="text"/> <br/>
    input 3 <input name="input3" type="text"/> <br/>
    input 4 <input name="input4" type="text"/> <br/>
    <input type="submit" value="go"/>

</form>

</body>
</html>

观看2:

<!DOCTYPE html>
<html lang="en">

</head>
<body>
page 2

<form method="get" action="<?= base_url('welcome/page2') ?>">
    input 1 <input name="input5" type="text" /> <br/>
    input 2 <input name="input6" type="text" /> <br/>
    input 3 <input name="input7" type="text" /> <br/>
    input 4 <input name="input8" type="text" /> <br/>
    <input type="submit" value="go"/>
    <a type="button" href="<?= base_url('welcome/page2') ?>">Back</a>
</form>

</body>

观看3:

<html>
<head>
  <title></title>
</head>
<body>
<form method="get" action="<?= base_url('welcome/page3') ?>">
<input type="submit" value="Save"/>
<a type="button" href="<?= base_url('welcome/page1') ?>">Back</a>
</form>
</body>
</html>

2 个答案:

答案 0 :(得分:0)

在构造

中加载库会话
class Welcome extends CI_Controller {

public function __construct()
{
    parent::__construct();
    $this->load->library('session');
}

在applications / config / autoload.php

中打开autoload.php
$autoload['libraries'] = array('session');

答案 1 :(得分:0)

会话数据是您遇到的最少问题。

首先,$this->load->view('pg2');不会在控制器中调用您的其他功能,而是会加载名为&#39; pg2&#39;的视图。在您的问题中,您没有显示您的观点的名称,但是如果查看1&#39;是/ views文件夹中名为pg1.php的文件,您的控制器函数可能如下所示:

public function pg1() {
        $this->session->set_userdata('page1',$this->input->get());
        $this->load->view('pg1');
}

然后你必须学习如何传递数据以便在视图中显示...尝试阅读文档,例如,这里有关于视图如何工作的页面:https://www.codeigniter.com/user_guide/general/views.html