CodeIgniter Forums
problem regarding session problem . i can`t store session value passing through jquery ajax. - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: problem regarding session problem . i can`t store session value passing through jquery ajax. (/thread-53015.html)

Pages: 1 2


problem regarding session problem . i can`t store session value passing through jquery ajax. - El Forum - 07-06-2012

[eluser]Anchal Gupta[/eluser]
Hi everyone,
i have the problem regarding storing session value
i am not able to increment session count variable
which call through the jqery ajax with some value passing
to function and also
my previous set array which i store in session is change with the
the new one
what is my problem i point out step wise
1>when i use one controller function
startOnlineTest($testid=0)
i set session countnew as 0
Code:
$this->session->set_userdata('countnew',0);
and in view i use jquery to pass data to other function of same controller
Code:
public function ResponseOnline($qid,$response)
using change effect of jquery
Code:
echo "[removed]$(document).ready(function(){";
         foreach($question['childQuestion'] as $q=>$aq){  //
echo "\$(\"input:radio[name=qid-$q]\").live('change',function(){
var sr=\$(\"input:radio[name=qid-$q]:checked\").map(function() {
          return $(this).val();
          }).get().join();
       var qid = \$(\"input:radio[name=qid-$q]\").attr(\"qaid\");
       "; echo "\$.get('"; echo base_url();echo "testpapers/ResponseOnline/'+qid+'/'+sr,{},function(data)
{\$('#res').html(data)});
});";}
echo"});[removed]" ;
this is script is working fine

now the problem is this i get the session value overwrite by the current one although i
use array my code for ResponseOnline
is


Code:
public function ResponseOnline($qid,$response)
  {
  echo "this" .$this->session->userdata('countnew'); // this is not echo as set above by function  startOnlineTest($testid=0)[/color]
     i set session countnew as
     $this->session->set_userdata('countnew',0)
  
echo $d=$qid; // i know its no use but to save time as tested on $d

$s=$response;

if($this->session->userdata('countnew')==0)   //  algo for this function i check the                                      
                                               //countnew session varaible   if 0 do this
{                                                
$sc=$this->session->userdata('countnew');     // store the countnew variable
echo $sc=$sc+1;                               // increment the counter variable
$this->session->set_userdata('countnew',$sc);  // store it in session
echo "this is incrementes session value";    
echo $this->session->userdata('countnew');
$r2[$d]=$s;                               // store array value $r2 with key $d and value $s
$this->session->set_userdata('res',$r2);  //set this array value in session
}
else{                                  // if session countnew!=0 then do this
$r2=$this->session->userdata('res');  // first store $r2 as array return from session
$r2[$d]=$s;                         //then add value to this array                      

$this->session->set_userdata('res',$r2); // storing this array to session
}
echo '<pre>';
print_r($r2);       // printing the array
  

}


i get the result for say for first call is fine but for second call my value is overwrite session show Array([32323]=>23)) if i pass function (32323,23) if i pass (33,42)
i get Array([33]=>42) my old value is destroyed

[Please Help Me]


problem regarding session problem . i can`t store session value passing through jquery ajax. - El Forum - 07-07-2012

[eluser]InsiteFX[/eluser]
Code:
&lt;?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* ------------------------------------------------------------------------
* Created by phpDesigner 8.
* Date : 5/6/2012
* Time : 11:19:06 PM
* The Learn CodeIgniter Development Team.
* ------------------------------------------------------------------------
*
* Class MY_Session
*
* @package  Package  CodeIgniter
* @subpackage Subpackage session
* @category category session
* @author  Raymond L King Sr.
* @link  http://example.com
* ------------------------------------------------------------------------
* To change this template use File | Settings | File Templates.
* ------------------------------------------------------------------------
*/

/**
* ------------------------------------------------------------------------
* CI Session Class Extension for AJAX and Xajax method calls.
* ------------------------------------------------------------------------
*
* ====- Save as ./application/libraries/MY_Session.php -====
*/

class MY_Session extends CI_Session {

// --------------------------------------------------------------------

/**
  * sess_update()
  *
     * Do not update an existing session on ajax or xajax calls
     *
     * @access    public
     * @return    void
     */
    public function sess_update()
{
  $_ci = get_instance();

  if ( ! $_ci->input->is_ajax_request())
  {
   parent::sess_update();
  }
    }

// --------------------------------------------------------------------

/**
  * sess_destroy()
  *
     * Clear's out the user_data array on sess::destroy.
     *
     * @access    public
     * @return    void
     */
public function sess_destroy()
{
  $this->userdata = array();

  parent::sess_destroy();
}

} // End of Class.

/* ------------------------------------------------------------------------
* End of file MY_Session.php
* Location: ./application/libraries/MY_Session.php
* ------------------------------------------------------------------------
*/



problem regarding session problem . i can`t store session value passing through jquery ajax. - El Forum - 07-07-2012

[eluser]Anchal Gupta[/eluser]
thank-you for your help but how i will use this MY_Session class i mean how i use this class
and as i want to update the session but also restore the previous one

what i have try: i pass value to ResponseOnline($qid,$response) function via ajax jquery and store that
inforamtion in session as Array with having key=$qid and $value=$response
as my code explain above

what i want:
when i pass value to ResponseOnline($qid,$response)
say i pass this funtion 23,43 through ajax i should store in session as Array([23]=>43)
when i pass this functino 51,100 through ajax i should store in session as Array([23]=>43
[51]=>100)
when i pass this function 55,21 through ajax is should store in session as Array([23]=>43
[51]=>100
[55]=>21)
when i pass this function 55,20 through ajax i should store in session as Array([23]=>43
[51]=>100
[55]=>20)


what i get:

for first pass i get Array([23]=>43)
for second pass as above i get Array([51]=>100)
for third pass i get Array([55]=>21)
for fourth pass i get Array([55]=>20)



problem regarding session problem . i can`t store session value passing through jquery ajax. - El Forum - 07-07-2012

[eluser]InsiteFX[/eluser]
Just place it in your libraries folder it auto invokes the real ci session class.



problem regarding session problem . i can`t store session value passing through jquery ajax. - El Forum - 07-08-2012

[eluser]Anchal Gupta[/eluser]
[quote author="InsiteFX" date="1341728561"]Just place it in your libraries folder it auto invokes the real ci session class.
[/quote]
no change is their i still get the same error
i am placing code in ..application/libraries/MY_Session.php
or i should place it ..system/libraries/MY_Session.php
and may i have to load this $autoload['libraries'] = array('database', 'session', 'xmlrpc','my_session');?


problem regarding session problem . i can`t store session value passing through jquery ajax. - El Forum - 07-08-2012

[eluser]InsiteFX[/eluser]
You do not need to load the MY_Session, just what is below.

Code:
$autoload[‘libraries’] = array(‘database’, ‘session’, ‘xmlrpc’);

This may be your problem here. If statements do not like to process return values.
Code:
if($this->session->userdata('countnew')==0)   //  algo for this function i check the                                      
                                               //countnew session varaible   if 0 do this
// Try this
$countnew = $this->session->userdata('countnew');

if($countnew == 0)   //  algo for this function i check the                                      
                                               //countnew session varaible   if 0 do this



problem regarding session problem . i can`t store session value passing through jquery ajax. - El Forum - 07-08-2012

[eluser]Anchal Gupta[/eluser]
[quote author="InsiteFX" date="1341743331"]You do not need to load the MY_Session, just what is below.

Code:
$autoload[‘libraries’] = array(‘database’, ‘session’, ‘xmlrpc’);

This may be your problem here. If statements do not like to process return values.
Code:
$xy= $this->session->userdata('countnew');
echo $xy;  // here nothing is echo as i loss the all session data
if($this->session->userdata('countnew')==0)   //  algo for this function i check the                                      
                                               //countnew session varaible   if 0 do this
// Try this
$countnew = $this->session->userdata('countnew');

if($countnew == 0)   //  algo for this function i check the                                      
                                               //countnew session varaible   if 0 do this


[/quote]



this is not my problem. my problem is that on ajax jquery pass data i loss my all previous session value . that is all my prevoius session value get destroyed. And i get the some new value which i set using above function
please help me to remove this session update on ajax request


problem regarding session problem . i can`t store session value passing through jquery ajax. - El Forum - 07-08-2012

[eluser]InsiteFX[/eluser]
If it is still not working then you have some thing wrong in your code. The MY_Session stops the update of the session if it is an ajax call.

I have a whole menu system that uses ajax and I have no problems, so take a look at what you are doing in your code. Some place you are over writing your session data.

If you are updating the same session variable then it will get over-written.

And just a note the page will not update for viewing variables unless there is a page refresh.



problem regarding session problem . i can`t store session value passing through jquery ajax. - El Forum - 07-09-2012

[eluser]Anchal Gupta[/eluser]
[quote author="InsiteFX" date="1341753831"]If it is still not working then you have some thing wrong in your code. The MY_Session stops the update of the session if it is an ajax call.

I have a whole menu system that uses ajax and I have no problems, so take a look at what you are doing in your code. Some place you are over writing your session data.

If you are updating the same session variable then it will get over-written.

And just a note the page will not update for viewing variables unless there is a page refresh.
[/quote]
i have check code this work fine with the core php but when i use your code MY_SESSION then what i found my all previous session value get destroyed.
suppose i store in session two value
when i want to add more value to session with your MY_session class i loss my all session value i get one value in session which i have added by function
but when i use without MY_session class


i get this error

what i want:
when i pass value to ResponseOnline($qid,$response)
say i pass this funtion 23,43 through ajax i should store in session as Array([23]=>43)
when i pass this functino 51,100 through ajax i should store in session as Array([23]=>43
[51]=>100)
when i pass this function 55,21 through ajax is should store in session as Array([23]=>43
[51]=>100
[55]=>21)
when i pass this function 55,20 through ajax i should store in session as Array([23]=>43
[51]=>100
[55]=>20)


what i get:

for first pass i get Array([23]=>43)
for second pass as above i get Array([51]=>100)
for third pass i get Array([55]=>21)
for fourth pass i get Array([55]=>20)



any one please help me out



problem regarding session problem . i can`t store session value passing through jquery ajax. - El Forum - 07-09-2012

[eluser]InsiteFX[/eluser]
MY_Session does not destory your data your code is wrong some place I use the MY_Session all the for years now and never have problems with it.