CodeIgniter Forums

Full Version: Creating Just A Session From Form Submit (Question - no code)
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]Barwick[/eluser]
Question - I need to create a session variable via form. I know how to do that, easy peezy. However, my question is, is it good practice to do this through a model (even though no DB interaction is taking place?) or is it okay to create the session function in the controller?

Cheers,
M

El Forum

[eluser]adamck[/eluser]
If the form is a login form without a DB then you will have to store the user info hard coded into your controller or view... this is bad practise.
I cant see why you need to set session data from any form other than a login form? Unless its a shopping cart, in which you can use the Cart library OR a cookie / similar.

What does the form do? you may be able to pass or store the data in a different way.

I cant see why setting a session from the controller could be an issue, for what i know... models are mainly for DB functions, unless its a function you will be re-using in other controllers... in which case it should be in a model and you should load the model and run the function passing variables to it.

example

Controller 1
Code:
function home()
{
$name = 'mike';
$this->load->model('home_model');
$this->home_model->set_session($name);
$this->load->view('home');
}

Controller 2
Code:
function new()
{
$name = 'john';
$this->load->model('home_model');
$this->home_model->set_session($name);
$this->load->view('new');
}

Home Model
Code:
function set_session($name)
{
$newdata = array(
                   'username'  => $name
               );
$this->session->set_userdata($newdata);
}

The above makes more sense as your doing 1 function twice, rather than writing the session function every time you need it.
Adam.

El Forum

[eluser]Barwick[/eluser]
No, not a login form. I want to set some flash data. Here's the feature I'm currently building...

On a wizard/set-up page within my application, I have an live preview area (box) where a user can enter in their web url and on submit, their website is embedded (via iFrame) within the Live Preview box so they can see how the "thing they're building" looks on their site. This entire Live Preview area of the wizard is all Ajax.

So my thoughts initially were to send the WEB URL form input to a controller, assign it to a session, then output the session variable in the iFrame url (aka the new DIV shown on successful form submit - ajax).

That make sense? lol

El Forum

[eluser]adamck[/eluser]
Makes sense... by why??

if its all Ajax why not use Jquery to update or update the iFrame...?

Something like this

Code:
$('#preview_button').click(function() {
    $('#my_iframe').attr('src', $('#website_url').val());
});

Now when the user enters a URL and clicks the preview button the iFrame's source is the value entered into the URL box.

El Forum

[eluser]Barwick[/eluser]
[quote author="Barwick" date="1352821875"]No, not a login form. I want to set some flash data. Here's the feature I'm currently building...

On a wizard/set-up page within my application, I have an live preview area (box) where a user can enter in their web url and on submit, their website is embedded (via iFrame) within the Live Preview box so they can see how the "thing they're building" looks on their site. This entire Live Preview area of the wizard is all Ajax.

So my thoughts initially were to send the WEB URL form input to a controller, assign it to a session, then output the session variable in the iFrame url (aka the new DIV shown on successful form submit - ajax).

That make sense? lol[/quote]

Also debating just doing this via jQuery...I don't need to save or store the into here.

El Forum

[eluser]Barwick[/eluser]
[quote author="adamck" date="1352822222"]Makes sense... by why??

if its all Ajax why not use Jquery to update or update the iFrame...?

Something like this

Code:
$('#preview_button').click(function() {
    $('#my_iframe').attr('src', $('#website_url').val());
});

Now when the user enters a URL and clicks the preview button the iFrame's source is the value entered into the URL box.[/quote]

Hmmm...I have two DIVs, one with the "form" and one to embed the iFrame. Think this might be the better route. Just show/hide the divs, etc.

El Forum

[eluser]adamck[/eluser]
Heres an example for you...


http://tinkerbin.com/GKqk9O7p


Just click 'Run' then type in a URL 'http://www.ebay.co.uk' and hit preview.

Some nice effects included Wink

El Forum

[eluser]Barwick[/eluser]
Cheers! Thanks for the help man. I just realized I actually need the input data...so I have to go the controller route now. But like the simplicity of this...much better!!

El Forum

[eluser]adamck[/eluser]
You can post via JQUERY too...

This will show a preview and submit the form without leaving the page... All done with Ajax POST.
You just need to extend it to have all your fields in it.

Code:
$('#preview').click(function() {
  $('#my_iframe iframe').attr('src', $('#url').val());
  $('#my_iframe').slideToggle(600).fadeIn('slow');
  $.post("<?php echo base_url(); ?>index.php/controller/function", {url: $("#url").val()});
});

El Forum

[eluser]adamck[/eluser]
Or you can have a preview button and a submit button so the user can preview and submit the form to the controller when happy...