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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T09:03:53+00:00 2026-06-18T09:03:53+00:00

My boss has tasked me with adding some Javascript to a couple of pages

  • 0

My boss has tasked me with adding some Javascript to a couple of pages to the website we’re developing. I’m a total Javascript noob (I’m a PHP coder at heart), but I’ve been learning and using Javascript for two days and until now, everything was going pretty smoothly.

Now I’m running into a problem. I think it’s related to variable scope, and I thought I understood variable scope, but I’m struggling with this very (probably) very simple task. If I had more time to learn how to use JS, I’d bang my head against it some more before coming to you folks, but I need something workable by next Monday. I’m close..

Code is below, explanation further below.

var activeTopic;
$.post("test.php",
    {
        request:"getActiveSection",
        courseid:courseid
    },
    function (data, status) {
        if (data != "none") {
            activeTopic = data;
            displayActiveTopic();
        }
    });
alert(activeTopic);

I’m using Jquery’s POST function to make a call to test.php. PHP takes the course ID and returns the currently active topic. The PHP code is returning the correct value. I know this, because if I alert(data);, the correct value is displayed.

However, if I alert(activeTopic); outside the function, I notice the global variable activeTopic hasn’t changed. Why?

Thanks in advance for the help. When I have some time, I’m going to put a bunch more effort into learning JS. It’s a lot of fun so far… very different from PHP, but fun nonetheless.

  • 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-06-18T09:03:54+00:00Added an answer on June 18, 2026 at 9:03 am

    This has nothing to do with scope and everything to do with timing.

    1. You declare a variable activeTopic.
    2. You send an HTTP (post) request
    3. You alert activeTopic

    Later, when the HTTP response arrives, a value is assigned to activeTopic.

    You need to deal with the data inside the callback function that currently assigns a value to activeTopic.

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

Sidebar

Related Questions

I'm not some kind of hax0r or so, but my boss has a website
I currently have a Rails website that has some Prototype scripting in it. My
My boss has seen this on a website: http://event.photoswarm.com/ Notice how the images change
I am a computer science student currently doing an internship. My boss has asked
My boss is running a laptop which has a setting which is resizing font
My boss is asking me to code a report that has the following components:
My boss wants me to encrypt some information used during data transfer. The individual
My boss wants me to develop some apps and make them appear on the
I am using TFS 2008 and Visual Studio and my boss has instructed me
I have been tasked with improving the current mess that is our JavaScript strategy;

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.