0

I want to retrieve the session value and pass that value as required parameter for the api. My session is $_SESSION['name']. And these are my codes

var uniqueName = **Here I want to retrieve session value**;
  if(**Then I want to check if uniqueName is null or undefined**) {
    $.ajax({
      url: 'http://api/check.php',
      type: 'GET',
      crossDomain: true,
      dataType:'json',
      data: {
        name: uniqueName
      },
      success: function(result) {
        alert("Success");
      },
      error: function(result) {
        alert("Error");
      }
    });
  }

  return false;
});
Patrick Q
  • 6,373
  • 2
  • 25
  • 34
F.Duh
  • 53
  • 1
  • 1
  • 8
  • In addition to the answer below, for your `if(**Then I want to check if uniqueName is null or undefined**)` ... just do `if (typeof uniqueName != 'undefined' && uniqueName !== null)` ... but since you have `var uniqueName = '';` above that, it will never be undefined ;) – IncredibleHat Dec 07 '17 at 18:29
  • 1
    In addition to @Randall comment above, you could use `uniqueName.length > 0` as your conditional instead – miknik Dec 07 '17 at 18:38

2 Answers2

2

You can put PHP directly into your JS, like

var uniqueName = '<?php echo isset($_SESSION['name']) ? $_SESSION['name'] : '' ?>';

In JS will be name or empty string.


If you haven't this JS in PHP file, you can pass a session using AJAX call, or assign to any HTML element.

<input type="hidden" id="session_var" value="<?php echo isset($_SESSION['name']) ? $_SESSION['name'] : '' ?>">

And in JS file then:

var name = document.getElementById('session_var'); // name from session or empty string.
pavel
  • 26,538
  • 10
  • 45
  • 61
  • If his JS is in a dedicated .js file included in the head, then he would have to do an ajax call to grab the session name value, with a promise to wait for its return before sending it off to the second ajax. OR he stuffs it into a basic html element that the js will pull from by element id and attr. Couple ways to go about it. – IncredibleHat Dec 07 '17 at 18:34
  • Could you please provide an example for that? @Randall – F.Duh Dec 07 '17 at 18:43
  • 1
    @F.Duh for what? If it's really JS file, easiest is to write SESSION into any element in HTML like ``. In JS then `var name = document.getElementById('session_var').innerHTML;` – pavel Dec 07 '17 at 18:45
  • What @panther said. Easiest is to insert it into hidden html elements. I do this a lot because ALL of my .js files are minimized and cached into the headers. I put all kinds of things in elements for js to grab at :D – IncredibleHat Dec 07 '17 at 19:02
  • @F.Duh I've updated my answer for the HTML/JS mix. AJAX request is another method, but these two are fine for you too. – pavel Dec 07 '17 at 19:06
0
var uniqueName = "<?php echo $_SESSION['name'] ?>";
  if(uniqueName) {
    $.ajax({
      url: 'http://api/check.php',
      type: 'GET',
      crossDomain: true,
      dataType:'json',
      data: {
        name: uniqueName
      },
      success: function(result) {
        alert("Success");
      },
      error: function(result) {
        alert("Error");
      }
    });
  }

  return false;
});

References:

Is there a standard function to check for null, undefined, or blank variables in JavaScript?

http://www.dyn-web.com/tutorials/php-js/scalar.php