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 7410965
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T06:18:50+00:00 2026-05-29T06:18:50+00:00

I am somewhat new to JavaScript, and have been having an issue with a

  • 0

I am somewhat new to JavaScript, and have been having an issue with a global variable. I have a group id that is part of my php session variables, that I want to place into a JavaScript global variable so that I can access the value in JavaScript. But JavaScript always shows the variable as undefined, so I must be doing somthing wrong. In a nutshell I have the following code:

<head>
   <script type="text/javascript">var group = <?php echo($group)?>;</script>
</head>
<body>
<script type="text/javascript">
   function clicked() {
      alert(window.group);
   }
</script>

When the alert window pops up it shows undefined, but I can tell by look at the source generated, as well as firebug that the value is being set to the variable at one point.

What am I doing wrong?

  • 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-29T06:18:51+00:00Added an answer on May 29, 2026 at 6:18 am

    The best solution for this kind of problem is to always call json_encode when printing a php variable as a JS variable. In your case, it would fail if your string contained a newline, or a quote

    <script type="text/javascript">var group = <?php echo(json_encode($group)) ?>;</script>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm somewhat new to Javascript, not HTML/CSS or PHP/MySQL, just JS. I have a
So I am still somewhat new to PHP, MySQL, and Javascript but I have
I have the following javascript object, somewhat pseudocode: { dateField: new Date(), addMinutes:function(numMinutes) {
I think that Im missing some pieces for this. Im somewhat new to JavaScript
I am somewhat new to PHP coding and I am aware that malicious users
I'm somewhat new to object oriented programming in Javascript and I'm trying to build
I am somewhat new to LINQ and have a quick question regarding deleting. Say,
I am somewhat new to R, and i have this piece of code which
I am somewhat new to jQuery and I have a problem with something I
I am somewhat new to PHP, and I am wondering: How important is it

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.