Home Retrieve the session value and pass as required parameter of api Ajax
Reply: 2

Retrieve the session value and pass as required parameter of api Ajax

F.Duh
1#
F.Duh Published in 2017-12-07 18:19:55Z

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;
});
panther
2#
panther Reply to 2017-12-07 19:04:43Z

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.
Rohit
3#
Rohit Reply to 2017-12-07 18:33:43Z
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

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.297455 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO