AJAX setting session variables doesnt work.
Im trying to set a session variable using AJAX but it doesnt seem to work:
This is my PHTML page:
<?php
session_start();
?>
//SOME CODE
<script type="text/javascript">
$j(document).ready(function()
{
var variablecalle=$j("#listadetiendas").val();
var data = 'calle='+variablecalle;
$j.ajax({
url: '../../updatesession.php',
type: 'POST',
data: data
})
});
Then this is my "updatesession.php" page:
<?php
session_start();
$_SESSION['calle']=$_POST['calle'];
?>
riahc3
1,282 posts since May 2008
Reputation Points: 62
Solved Threads: 13
Skill Endorsements: 11
Something is wrong with the PHP code so I put:
<?php
session_start();
echo ("post of calle ". $_POST['calle'] );
echo ("session id is ". session_id());
?>
riahc3
1,282 posts since May 2008
Reputation Points: 62
Solved Threads: 13
Skill Endorsements: 11
New code:
$j(document).ready(function()
{
var variablecalle=$j("#listadetiendas").val();
var currentSessionID = "<?php echo session_id(); ?>";
var data = encodeURIComponent('calle='+variablecalle);
//var data = 'calle='+variablecalle;
/*$j.ajax({
url: '../../updatesession.php',
type: 'POST',
data: data
}) */
$j.ajax({
url: '../../updatesession.php',
type: 'POST',
data: data
}).done(function(data){
var retData = JSON.parse(data);
if(retData.sessID != currentSessionID){
alert("doesn't have an equal session id. \n current: " + currentSessionID + "\nIDinAjaxPage: " + retData.sessID);
}
if(!retData.calleSet){
alert("calle was not set. due to wrongid");
}
alert("sent calle: " + encodeURIComponent('calle='+variablecalle)+ "\nRecieved calle: " + retData.callePostVal);
});
});
PHP:
<?php
session_start();
echo ("post of calle ". $_POST['calle'] );
echo ("session id is ". session_id());
?>
Firebug says:
SyntaxError: JSON.parse: unexpected end of data
var retData = JSON.parse(data);
riahc3
1,282 posts since May 2008
Reputation Points: 62
Solved Threads: 13
Skill Endorsements: 11
Same errror.
Here is the code again:
$j(document).ready(function()
{
var variablecalle=$j("#listadetiendas").val();
var currentSessionID = "<?php echo session_id(); ?>";
var data = encodeURIComponent('calle='+variablecalle);
//var data = 'calle='+variablecalle;
/*$j.ajax({
url: '../../updatesession.php',
type: 'POST',
data: data
}) */
$j.ajax({
url: '../../updatesession.php',
type: 'POST',
data: data
}).done(function(data){
var retData = JSON.parse(data);
if(retData.sessID != currentSessionID){
alert("doesn't have an equal session id. \n current: " + currentSessionID + "\nIDinAjaxPage: " + retData.sessID);
}
if(!retData.calleSet){
alert("calle was not set. due to wrongid");
}
alert("sent calle: " + encodeURIComponent('calle='+variablecalle)+ "\nRecieved calle: " + retData.callePostVal);
});
});
PHP
<?php
session_start();
//echo if the calle session do exists
echo '{' . '"calleSet":' . (isset($_SESSION['calle'])?'true,':'false,')
//echo the sessID
. '"sessID":' . '"'. session_id() .'",'
//resend the post value of calle
. '"callePostVal":' . '"'. (isset($_POST['calle'])?$_POST['calle']:'') .'"}';
?>
riahc3
1,282 posts since May 2008
Reputation Points: 62
Solved Threads: 13
Skill Endorsements: 11
Instead of building the JSON response with echo and string concatenation, I suggest building an array and using json_encode() on it.
pritaeas
Posting Prodigy
9,265 posts since Jul 2006
Reputation Points: 1,173
Solved Threads: 1,456
Skill Endorsements: 86
I put a alert(data); before
var retData = JSON.parse(data);
And it returns nothing. Empty.
riahc3
1,282 posts since May 2008
Reputation Points: 62
Solved Threads: 13
Skill Endorsements: 11
Also did a alert of variablecalle and it has a value.
riahc3
1,282 posts since May 2008
Reputation Points: 62
Solved Threads: 13
Skill Endorsements: 11
Instead of building the JSON response with echo and string concatenation, I suggest building an array and using json_encode() on it.
What is the best/correct way to do this?
riahc3
1,282 posts since May 2008
Reputation Points: 62
Solved Threads: 13
Skill Endorsements: 11
Change that and now it gives me:
TypeError: retData is null
if(retData.sessID != currentSessionID)
riahc3
1,282 posts since May 2008
Reputation Points: 62
Solved Threads: 13
Skill Endorsements: 11
New code:
$j(document).ready(function()
{
var variablecalle=$j("#listadetiendas").val();
var currentSessionID = "<?php echo session_id(); ?>";
var data = encodeURIComponent('calle='+variablecalle);
//var data = 'calle='+variablecalle;
/*$j.ajax({
url: '../../updatesession.php',
type: 'POST',
data: data
}) */
$j.ajax({
url: '../../updatesession.php',
type: 'POST',
data: data
}).done(function(data){
$j(document.body).append("currSessID: |"+currentSessionID+"| data |"+data+"|");
var retData = $j.parseJSON(data);
if(retData.sessID != currentSessionID){
alert("doesn't have an equal session id. \n current: " + currentSessionID + "\nIDinAjaxPage: " + retData.sessID);
}
if(!retData.calleSet){
alert("calle was not set. due to wrongid");
}
alert("sent calle: " + encodeURIComponent('calle='+variablecalle)+ "\nRecieved calle: " + retData.callePostVal);
});
});
Result:
currSessID: |3j5ncc0m9uggb34bisjdmogr96| data ||
riahc3
1,282 posts since May 2008
Reputation Points: 62
Solved Threads: 13
Skill Endorsements: 11
Thank you pritaeas and gon1387.
gon1387, Im going to adapt the code you posted to what I have....
Might take a minute or two.
riahc3
1,282 posts since May 2008
Reputation Points: 62
Solved Threads: 13
Skill Endorsements: 11
Still nothing. Odd.
PHTML:
start
<div id="currSess"></div>
finish
<script type="text/javascript">
$j(document).ready(function()
{
var variablecalle=$j("#listadetiendas").val();
//var currentSessionID = "<?php echo session_id(); ?>";
var data='calle='+encodeURIComponent(variablecalle);
//var data = encodeURIComponent('calle='+variablecalle); <---------big difference between this and line before it
//var data = 'calle='+variablecalle;
$j.ajax({
url: '../../updatesession.php',
type: 'POST',
data: data
}).done(function(data){
//append the recieved data on success
$j("#currSess").html(data);
});
});
</script>
PHP:
<?php
/*session_start();
$_SESSION['calle']=$_POST['calle'];
$_SESSION['fecha']=$_POST['fecha'];*/
/*session_start();
//echo if the calle session do exists
echo '{' . '"calleSet":' . (isset($_SESSION['calle'])?'true,':'false,')
//echo the sessID
. '"sessID":' . '"'. session_id() .'",'
//resend the post value of calle
. '"callePostVal":' . '"'. (isset($_POST['calle'])?$_POST['calle']:'') .'"}';*/
session_start();
$f = new StdClass();
$f->calleSet= isset($_SESSION['calle'])?true:false;
$f->sessID = session_id();
$f->callePostVal = isset($_POST['calle'])?$_POST['calle']:'';
echo json_encode($f);
?>
Im kinda of confused now :S
riahc3
1,282 posts since May 2008
Reputation Points: 62
Solved Threads: 13
Skill Endorsements: 11