• 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

    • Apart from a few very specific hiccups, Windows 11 has been good for me. Rock solid and stable. Unpopular opinion, I know, but it's mine.
    • No. Windows 8 had little to no spywhare, is actually faster than 7 but it has the dreaded start screen, nothing that you could not fix with start8. Windows ME crashed often, but was shotlived and replaced by XP, which is a much better windows in itself.
    • Windows 8 was uninspiring but it was fine / worked. It was just overshadowed by how great Windows 7 was. Windows ME seem to be just unstable for most people - I was using Windows 2000, which was far superior. The two best versions are Windows 7 and 2000
    • I don't think ME was that bad. The stability issues were mostly caused by drivers. This was when the switch from vxd to wdm was happening. The other complaint was not being able to shut down into DOS anymore which was silly with XP right around the corner.
    • As much as I hated Windows 8, although I never used it personally on any of my own machines, I think Windows Me was hands down the worst OS from MS. I have Windows 11 on 2 unsupported machines and haven't had any issues and I actually kind of like it. Still hate how slow Windows updates are though.
  • Recent Achievements

    • Rookie
      +ChiefOfNeo went up a rank
      Rookie
    • One Month Later
      Tom Schmidt earned a badge
      One Month Later
    • Week One Done
      Tom Schmidt earned a badge
      Week One Done
    • One Year In
      Tom Schmidt earned a badge
      One Year In
    • One Month Later
      JimErn earned a badge
      One Month Later
  • Popular Contributors

    1. 1
      +primortal
      453
    2. 2
      +Edouard
      176
    3. 3
      PsYcHoKiLLa
      122
    4. 4
      Michael Scrip
      81
    5. 5
      Xenon
      75
  • Tell a friend

    Love Neowin? Tell a friend!