Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6906819
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T08:21:30+00:00 2026-05-27T08:21:30+00:00

How can I get my session data using javascript. First I open my php

  • 0

How can I get my session data using javascript.

First I open my php file (www.sample.com/index.php)

index.php

<? $_SESSION['user'] = "carlo34";?>

<script>var data = '<?php json_encode($_SESSION['user']) ?>';</script>

Then I open my widget (windows 7 gadget)

Then I have a function

test.js

$.get('index.php', function(data)){

alert(data);

});

but it doesn’t alert any.

/////
Thanks thats a helpful tip, but on my case still didn’t work. I just stated some details, sorry about that. I am displaying my session variable on the Client side, I have a script that would load another script on the domain

my.js(client side)
here it will load my script

var url = "http://www.sampledomain.com/test.js";
var script = document.createElement('script');
script.src = url;
document.head.appendChild(script);

test.js(domain side)

$.get('index.php',function(data)){

alert(data);
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T08:21:31+00:00Added an answer on May 27, 2026 at 8:21 am

    You’re doing it wrong. First of all, don’t echo PHP data directly into javascript. It’s far too easy to produce a JS syntax error and kill the rest of your Javascript code.

    <script>var user = <?php echo json_encode($_SESSION['user']) ?>;</script>
    

    using json_encode guarantees you get syntactically correct javascript, regardless of what’s stored in that variable.

    Beyond that, once you’ve done this echo, you don’t need to do any AJAX calls, the value is now JS code as far as the browser is concerned, and you just do:

    alert(user);
    

    For an actual AJAX response, in the way you want to treat it, you don’t output any <script> tags and the like. For a simple “make a request and get a string back”, you’d have your PHP script do:

    <?php
        echo 'this is some data';
    ?>
    

    and on the JS side:

    $.get('script.php', function(data) { 
         alert(data);
    });
    

    would pop up an alert box with “this is some data” in it. If you output <script> tags and then do the same alert, you’ll get <script>this is some data</script> in the alert, etc…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using Flash uploader(uploadify, swfupload) with CodeIgniter, want to get the session data. I
I'm using the Facebook php-sdk to get the users name and friends, right now
I am trying to get my status from facebook using the javascript api. I
I'm using express framework. I want to reach session data from socket.io. I tried
I am trying to implement a DTLS server using OpenSSL. I can get app
I am using spring mvc 3.0 to build an web application. User can get
I've started using the AspProviders code to store my session data in my table
Can get all triples with value null in specific field? All people with date_of_birth
I can get easily see what projects and dlls a single project references from
I can get both System.Net.Mail and System.Web.Mail to work with GMail, but I can't

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.