Welcome Guest, Not a member yet? Register   Sign In
How to efficiently use CI/jQuery/ajax together
#1

[eluser]Unknown[/eluser]
Greetings all,

What I am essentially trying to do is use ajax from jquery to check to see if a user is logged in or not. For my login session, I have the variable 'is_logged_in' set to TRUE once the user logs in.

I have a div with an id of 'login_form':
Code:
<div id="login_form">

<h1>Please login below</h1>
    &lt;?php
echo form_open('login/validate_credentials');
echo form_input('username', 'Username', 'id="username"');
echo form_password('password', 'Password', 'id="password"');
echo form_submit('submit', 'Login', 'id="login_button"');
echo anchor('login/signup', 'Create Account', 'id="create_account"');
echo form_close();
?&gt;

</div>

and this is initially set to be hidden. What I would like to do is once '#login_menu' is clicked, check to see if the user is logged in. If they aren't logged in, I would like to display the "login_form" above (in jQuery
Code:
$('#login_form').addClass('active');
) else, do nothing.

So how can I use ajax in jQuery to request if the session variable 'is_logged_in' is false, then addClass 'active' ?

Thanks for your help!
#2

[eluser]PhilTem[/eluser]
Google "jQuery read session" or search the jQuery forums since this is about CI (server side) and not about jQuery (client side).

But anyway, this code snippet might help

Code:
var isCoBrowse = <%= Session["Name"].ToString().ToLower() %>;
if( ! isCoBrowse) //disable controls
  $(":text,:checkbox,:radio").attr("disabled","disabled"); //standard.
#3

[eluser]Unknown[/eluser]
So, for example,
Code:
var logged_in = <%= Session["is_logged_in"] %>;

should return me the result I want? This doesn't work for me.
#4

[eluser]Aken[/eluser]
Yeah that's definitely not PHP, so won't really work in CI...
#5

[eluser]Pedroshow[/eluser]
you can make a jQuery request, eg:

Code:
$.post('/users/loggedIn', {}, function(data){
if (data.loggedIn)
  alert('online!');
}, "json");

and use it on your controller

Code:
public function loggedIn() {

// do your work here
$data = array(
  'loggedIn' => true,
  'fullName' => 'Pedro Maia'
);

$this->output->set_content_type('application/json')
       ->set_output(json_encode($data));
}

I've made a fully CI application with codeigniter, I can give you some tips.




Theme © iAndrew 2016 - Forum software by © MyBB