3

So I'm trying to output a session's variable value into a view (only if it exists). But I can't seem to get it to work. No error. Nothing. What am I missing? Thought I had this down packed by now - guess not...

In controller...FYI, $data is assigned to the view (i.e. $this -> load -> view('view', $data);

$data['campaign_name'] = $this -> session -> userdata('campaign_name');

Here's my php snippet in the view that I'm trying to output. So in short, if the session exists, output it. If not, do nothing.

<input type="text" name="campaign_name" class="wizardInput nameField" value="<? if (isset($campaign_name)) ;?> ">

Anyone?

EDIT Okay, I should have mentioned that i'm trying to output the session value into a FORM value. Modified view code above. The form submits as though the value is there - and even sends the value along. However, it's not visible in the text input...

1 Answer 1

8

you can easly do this in your view:

if($this->session->userdata('campaign_name')){
 // do somenthing cause it exist
}

then if you want to make session data as the value of an input do this:

<input type="text" name="campaign_name" class="wizardInput nameField" value="<?php echo $this->session->userdata('campaign_name') ?>">

you don't need to control if session userdata exist cause if it not exist it doesn't prints anything, cause userdata() method returns false !

Then you don't need to pass session data trough the $data[] array, cause session data can be retrieved from anywhere (model/controllers/views/hooks/libraries/helpers and so on)

Sign up to request clarification or add additional context in comments.

8 Comments

Is this not the dirty way? I thought this was looked down upon. No? Check out my edit above...the input value is reading the session value, but it's visible for some reason...what the...
Should also mention, I want to INSERT the sessions value into the Text Input Value.
if you want to make input hidden you should use type="hidden" then
Awesome. Say I have a default value already in the text input. What then? If session exists, it should override...
Input doesn't need to be hidden. This is just used if a user goes "back" to the preview page in the ajax wizard I'm creating. Last step in wizard I send to database and kill this userdata.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.