Welcome Guest, Not a member yet? Register   Sign In
Creating Just A Session From Form Submit (Question - no code)
#1

[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
#2

[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.
#3

[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
#4

[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.
#5

[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.
#6

[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.
#7

[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
#8

[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!!
#9

[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()});
});
#10

[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...




Theme © iAndrew 2016 - Forum software by © MyBB