• 0

Declare a global variable I can use all over a website?


Question

Hey

I want to declare a variable that is global that I can use (and modify) all over my website.

It is a simple HTML textbox that I use when I click a "button" (it is designed to look like a button) and when I click it, I execute a Javascript function and that is where I also want to create/modify and store the variable which is the value in the HTML textbox

My main problem lies that Im trying to create a client side languge based (JS) global variable and then trying to access in server side language based (PHP) pages. I have to be able to access it from other JS, PHP, PHTML, HTML, etc. pages.

A cookie comes to mind but I tried "document.cookie" and it doesnt work; I suppose that it only applies to the CURRENT document at hand. The other option Ive read about is using Ajax but I have no idea how to do it and for ONE thing I perfer another solution almost :)

Thanks for all the help!

6 answers to this question

Recommended Posts

  • 0

Given the developments of EU Cookie Law (that will come in force May 26th this year; I believe UK has already even ratified it), I'll be using database storage for sessions, and I recommend databases, written/read using AJAX, in this case as well, even though it may be quite inefficient approach.

  • 0

2 points on cookies, potentially of relevance to the OP:

Does this only affect websites hosted in the EU?

The location of your hosting is irrelevant, but the location of your organisation is not. Your organisation must fall within the legal jurisdiction of the EU. Each member state has their own laws, which are based on the same EU directive, but may differ slightly.

For most small/medium organisations, being located in the EU will mean you must comply.

Are all cookies affected?

The vast majority are - all cookies that are not "strictly necessary for a service requested by a user".

The law allows an exception for "strictly necessary" cookies, such as those used to remember when something has been added to a shopping basket. These cookies would be expected by the user implicitly for the action they requested to be carried out. Another example would be login.

http://silktide.com/cookielaw

I.e. if the OP is outside the EU, or if the cookie is necessary, it's fine to use a cookie.

  • 0

I think I'll actually have to read that ebook they have there (or even the law itself, if available anywhere). It's all confusing, with those exceptions. Currently I'd rather remove functionality than have anything to do with it. :s

Although I'm not impartial to the problem EU wanted to address.

  • 0

Cookies are one solution, assuming the cookie is "essential to correct functioning of the website" (non-essential cookies for, say, tracking user movement or analytics, will require an opt-in message for EU users). They're pretty easy to use, and most, if not all people now, have them enabled. This'd be how I'd do it.

AJAX is another option. You'd store the "global variable" as a session variable, and then create an AJAX function to request the variable's value from a PHP page, which will then be returned in the AJAX response. This however is extremely slow compared to the cookie method, as it requires a full round trip to the server. It also requires that the user have JS enabled, and if the variable value is required for the site to work correctly, users with JS disabled are going to have a bad time.

=====

With regards to the EU cookies law, not all cookies are covered. Cookies that are essential to the running of the site (i.e. if the user doesn't opt-in, the site will be FUBAR) are allowed without having to prompt the user, however non-essential cookies (tracking cookies, analytics, theming, etc) will require an opt-in message to appear for the user. Being outside the EU isn't a "get out of jail free" card either. Any website any EU visitors must have it enabled (it's where the user lives, not where you live, that matters).

  • 0

This law does not affect me in any way shape or form.

This is basically what Ive done/tried so far:

For cookies creation in JS I based myself off http://www.w3schools.com/js/js_cookies.asp

In my Javascript:

document.cookie="textname=" + document.getElementById('textname').value;

With my textbox being declared as (HTML):

<input type="text" id="textname" name="textname"/>

I KNOW this works because right after that document.cookie line I put

alert(textname);

And it showed me the value correctly....

Now on to a completely different PHP page (PHTML to be exact):

/*random stuff*/
<?php
session_start();
?>
/*more HTML PHP etc..........*/
<?php echo $_COOKIE["textname"];  ?>

And it simply shows blank. Nothing else.

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • I always thought the moon gets a lot of impact because there’s no atmosphere, so surely building a moon base is only going to end in disaster?
    • Gets them every time !  
    • This piece of ###### is probably one of the most hated apps that ever existed.
    • Microsoft is bringing a much-needed Recap app to Teams, here is a first look by Usama Jawad Microsoft Teams is heavily used in work and school environments, and perhaps one of its core but extremely useful features is the ability to record meetings. In past years, Microsoft has further improved upon this functionality by integrating AI, but you do need a Microsoft 365 Copilot license to leverage most, if not all, all of those capabilities. Now, the Redmond tech firm is making another significant enhancement in the area of Teams meeting recordings. Up until now, if you wanted to access Teams recordings, you had to arduously locate the meeting invite and navigate to the dedicated tab, or go to the cloud storage location such as a SharePoint site. This was a rather overwhelming activity, especially if you don't remember the name of the meeting or the meeting occurred quite a while ago. Microsoft is now attempting to solve this problem through a dedicated Recap app that consolidates all your recordings. This centralized experience will allow users to find all recordings from the past 30 days and also offer access to other related services such as transcripts and AI-powered summaries. Customers will have the option to search for recordings, filter them, and review multiple meetings by generating AI-powered podcast-style recaps. The Recap app will list all available recordings in both thumbnail and list views. The former is shown below: And here is how Teams users with a Microsoft 365 Copilot license can select multiple recordings to generate a podcast-style audio recap: Microsoft has emphasized that the Recap app is pre-installed in Teams but it will not be pinned by default. Users will able to navigate to the Teams app store from the left rail, and pin it from the apps section. It will be enabled by default for all users once it becomes available. It's worth noting that while Teams recordings and transcripts can be accessed by all users governed by existing permissions, AI-powered features like intelligent summaries, audio recaps, and video recaps will require a Microsoft 365 Copilot license. The Recap app will be generally available to Teams users on Windows, Mac, and the web by the end of next month, with mobile support coming soon.
    • It's so stupid that you have to "enroll" in these extended updates.
  • Recent Achievements

    • Week One Done
      tuben earned a badge
      Week One Done
    • First Post
      OffsetAbs earned a badge
      First Post
    • Reacting Well
      OffsetAbs earned a badge
      Reacting Well
    • First Post
      Kolakid60 earned a badge
      First Post
    • Week One Done
      xvvxcvv earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      424
    2. 2
      +Edouard
      183
    3. 3
      PsYcHoKiLLa
      149
    4. 4
      Steven P.
      71
    5. 5
      FloatingFatMan
      71
  • Tell a friend

    Love Neowin? Tell a friend!