admin管理员组

文章数量:1279242

i want to set my session data in my view but Conditionally, so in javascript portion of my view i have

$('#demo').click(function(){
$this->session->set_userdata('sample',10);
});

However , whenever i refresh my page , without clicking the button , the session data , "sample" is set, is there a way i can get this session data set ONLY after i click the button ?

Thank you

i want to set my session data in my view but Conditionally, so in javascript portion of my view i have

$('#demo').click(function(){
$this->session->set_userdata('sample',10);
});

However , whenever i refresh my page , without clicking the button , the session data , "sample" is set, is there a way i can get this session data set ONLY after i click the button ?

Thank you

Share Improve this question asked Aug 14, 2012 at 7:35 Nishant JaniNishant Jani 1,9938 gold badges30 silver badges41 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 6

You're mixing Javascript and PHP (client-side and server-side). You can do it by using Ajax like this:

$('#demo').click(function(){
  $.ajax({
     type: "POST",
     url: "mycontroller/sessions"
   }).done(function( msg ) {
     alert( "Data Saved: " + msg );
   });
});

And in your Mycontroller.php file, you should create a function called "sessions":

function sessions(){
   $this->session->set_userdata('sample',10);
}

If you need to pass information from Javascript to PHP:

var dummy = 10;
$('#demo').click(function(){
   $.ajax({
      type: "POST",
      url: "mycontroller/sessions",
      data: { value: dummy }
   }).done(function( msg ) {
      alert( "Data Saved: " + msg );
   });
});


function sessions($value){
   $this->session->set_userdata('sample',$value);
}

More information about Ajax in JQuery http://api.jquery./jQuery.ajax/

EDIT:

To check if a session variable exists in CodeIgniter:

function sessions(){
   $sId = $this->session->userdata('session_id');
   if(isset($sId)){
      // session_id exist
   }
}

Section "Retrieving Session Data" http://codeigniter./user_guide/libraries/sessions.html

The following method returns FALSE if variable in session is not set

$this->session->userdata('sample')

Otherwise it returns the appropriate value.

本文标签: javascriptConditionally set session data in codeigniterStack Overflow