Welcome Guest, Not a member yet? Register   Sign In
Passing variables from javascript to CI
#1

[eluser]shiroin[/eluser]
Hi all,

New to CI and really enjoying coding with the MVC model. Actually trying to recode my whole site to fit the CI framework...

but I am encountering a big problem:

My site detects the user's screen resolution and arrange the layout accordingly.

Before I started using CI, I used javascript to detect user's screen resolution, send it to a cookie, and have PHP read the cookie and set it as a constant.

my code looked like this:
Code:
//cookie_constants.php

if(isset($HTTP_COOKIE_VARS["users_window_w1"])){
    define("SCREEN_W",$HTTP_COOKIE_VARS["users_resolution_w1"]);
    define("SCREEN_H",$HTTP_COOKIE_VARS["users_resolution_h1"]);
    define("WINDOW_W",$HTTP_COOKIE_VARS["users_window_w1"]);
    define("WINDOW_H",$HTTP_COOKIE_VARS["users_window_h1"]);
}
else //means cookie is not found set it using Javascript
{
?>
<scr1pt language="javascript">
&lt;!--
writeCookie();

function writeCookie(){
    var the_date = new Date();
    the_date.setTime(the_date.getTime()+60000);
    var the_cookie_date = the_date.toGMTString();
    var the_cookie1 = "users_resolution_w1="+ screen.width;
    var the_cookie2 = "users_resolution_h1="+ screen.height;
    var the_cookie1 = the_cookie1 + ";expires=" + the_cookie_date;
    var the_cookie2 = the_cookie2 + ";expires=" + the_cookie_date;
    [removed]=the_cookie1
    [removed]=the_cookie2
    location = '&lt;?=selfURL();?&gt;';
}
//--&gt;
</scr1pt>
&lt;?
}

Basically:
(cookie exists){
load cookies as PHP constants.
}else{
javascript detects screen resolution ans write cookie.
reload this PHP file.
}

Now I am trying to implement this into a model that gets loaded automatically (global_model.php), and I wish to load these cookies into CI Sessions.

How can I do this?

I've tried putting the whole thing into the global_model() constructor with no luck.

Or is there some other way I can detect screen resolution without using javascript?

Thanks soo much in advance.
#2

[eluser]garymardell[/eluser]
you dont need to set a cookie to get the data back to php, javascript can make requests such as post and get requests. This means you can get the resolution with javascript, have a controller function that gets post data, post the data back with javascript and then set your php session if you wish, or just use the data at runtime.
If you would like to cache the data then check if you already know the information and dont include the javascript file.
#3

[eluser]shiroin[/eluser]
[quote author="garymardell" date="1248933682"]you dont need to set a cookie to get the data back to php, javascript can make requests such as post and get requests. This means you can get the resolution with javascript, have a controller function that gets post data, post the data back with javascript and then set your php session if you wish, or just use the data at runtime.
If you would like to cache the data then check if you already know the information and dont include the javascript file.[/quote]

thank you for this suggestion. do you have an example that I can look at?

sorry I am fairly new with JS and CI, but I am willing to learn new techniques.

thank you once again,
#4

[eluser]devbro[/eluser]
you are coding in the wrong place. you should code it in view. View is the place that takes care of how things are displayed on the site.

In your controller detect the cookie data and pass them to views. and view should take care of the rest.




Theme © iAndrew 2016 - Forum software by © MyBB