Reputation: 12368
I am using sessions to make a multi-stage form, I want to record the information through each stage using codeIgniter sessions then input the session info all into the database at the end.
What I want to do is go from stage 1 in the form, enter a number into an input box, submit it, grab the input number through post and set that number from the post in the session. Then in stage 2 grab the information from the session and simply echo it, then at least I know it's working.
I had my code working earlier, but after moving it around and clearing the cache in chrome it suddenly stopped working and I can't see anything that's wrong with. Please note I'm working in chrome, but I've also tried this in firefox and ie. I am loading the session library with config/autoload and have my encryption key set in the config. I have tried closing chrome and reopening it. Any help would be most appreciated!
Controller: "scholarshiphistory.php"
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Scholarshiphistory extends CI_Controller {
public function __construct(){
parent::__construct();
$this->load->model('site_model');
}
//stage 1 of multipart form
public function addhist_selectstudent(){
$this->load->helper('form');
$data = $this->site_model->general();
$this->load->view('view_addhist_selectstudent',$data);
$this->load->view('view_footer',$data);
//set session info if user submits
if($this->input->post('studentSearch')){
$this->session->set_userdata('studentNationalId', $this->input->post('studentSearch'));
}
}
//stage 2 of multipart form
public function addhist_scholarshipdetails(){
if (!$this->session->userdata('studentNationalId')) {
//no session
$data["msg"] = "<strong>No session!</strong>";
} else {
//get the userinput from the session
$userinput = $this->session->userdata('studentNationalId');
$data["msg"] = "ID:". $userinput;
}
$this->load->view("view_addhist_scholarshipdetails",$data);
}
View: Stage 1 - "view_addhist_selectstudent.php"
<?php $formAttributes = array('role' => 'form', 'style' => 'width: 600px;'); ?>
<?php echo form_open('scholarshiphistory/addhist_scholarshipdetails', $formAttributes); ?>
<div class="form-group">
<div class="panel panel-default">
<div class="panel-body">
<div class="form-group">
<label for="studentSearch">Student:</label>
<input type="text" name="studentSearch" class="form-control" id="studentSearchInput" placeholder="student num" value="">
<br>
<span id="searchResult" class="help-block" ></span>
<button id="studentSearchBtn" class="btn btn-default">Search</button><!--search button returns info on user input in span above-->
</div>
</div>
</div>
<button id="studentSubmitBtn" type="submit" class="btn btn-default" >Next</button> <!--this is the submit button-->
</div>
</form>
View: Stage 2 - "view_addhist_scholarshipdetails"
<?php echo $msg; ?>
When I run the code I get to the view 'view_addhist_scholarshipdetails' and it shows the first branch of the if statement in the function addhist_scholarshipdetails in the controller, i.e "no session".
Upvotes: 1
Views: 3043
Reputation: 12368
I moved the code to set the session from the first method in the controller to the second method (also in the controller) and it works okay now, i.e. the session is getting set with the set_userdata method!
Second method now looks like this:
//stage 2 of multipart form
public function addhist_scholarshipdetails(){
if($this->input->post('studentSearch')){
$this->session->set_userdata('studentNationalId', $this->input->post('studentSearch'));
}
if (!$this->session->userdata('studentNationalId')) {
//no session
$data["msg"] = "<strong>No session!</strong>";
} else {
//get the userinput from the session
$userinput = $this->session->userdata('studentNationalId');
$data["msg"] = "ID:". $userinput;
}
$this->load->view("view_addhist_scholarshipdetails",$data);
}
The method I was trying previously seemed to work okay for this guy: http://runnable.com/UhIVTnEfFJEMAAB5 But I think it was because he wasn't setting his session in the same way I was.
Thank so much to those guys who responded to the question.
Upvotes: 0