-1

I have been through a ton of other posts and cannot figure out how to do this.

AJAX:

$.ajax({
   type: 'POST',
   url: 'getSessionVariable.php',
   contentType: 'application/json',
   dataType: 'json',
   success: function(data) {
     alert(data);
   },
   error:function(data){
      alert("error occured"); //===Show Error Message====
      alert(data); 
   }
});

PHP:

<?
   session_start();
   print_r(json_encode($_SESSION["Variable"]));
?>

This solution returns "error Occurred"

Second Ajax statement:

jQuery.ajax({
  type:'POST',
  url: 'getSessionVariable.php',
  contentType: 'json',
  success:function(data){ 
    alert("Sucess!");
    alert(data);
    },
  error:function(data){
    alert("error occured"); //===Show Error Message====
   // alert(data); 
}
});

The second AJAX solution runs successfully but returns the entire php page back, I am just looking for the session variable to be returned...

2 Answers2

0

It was because i was using a short tag. This oversight really hurt me this time, d'oh!

<? 

and not

<?php
0

Make sure you are use the corrects tags <?php ?> opener

And for use of <?= ?> that replace <?php echo "myString"; ?> you have to change your short_open_tag in php ini settings.

From PHP documentation you have:

Version 7.0.0 The ASP tags <%, %>, <%=, and the script tag are removed from PHP.

-

5.4.0 The tag <?= is always available regardless of the short_open_tag ini setting.

Change in your code the <? for <?php and to send json_encode you must use an echo istead of print_r

    <?php
       session_start();
       echo json_encode($_SESSION["Variable"]);
    ?>