• 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

    • Build your own business with a Sellful lifetime plan now at 76% off by Steven Parker Today's deal from our Apps + Software section of the Neowin Deals store, lets you save 76% off Sellful: ERP Agency Plan lifetime deal. AI-Powered Software and Website Builder for Agencies Ever feel like a client’s needs simply can’t be met on a single platform? With Sellful, it’s all here, and all white labeled. Build anything from simple websites to complex workflows to automate your business in a few clicks. Manage everything from email & social media marketing, to payroll & invoicing. It’s got a white label website builder, online shop, CRM, ERP, marketing, memberships, invoicing, appointments, online courses, project manager, and point of sale functions. Sellful is the only white label platform in the world that is truly all in one, combining all aspects of your business in one place no matter the industry. What can you do with Sellful? Automatically generate amazing websites, funnels, & landing pages in seconds using AI Sell physical & digital products online Keep track of customers with native CRM Automate communication & outreach using AI Manage all aspects of your business in one place Collect emails & phone numbers via forms on your website, then send newsletters to customers with important updates, sales, and discounts Build membership programs with various levels of access for your customers Receive payments from your clients using any number of payment gateways including Paypal, Stripe, Authorize.net, Square & more Have your clients book appointments for services & meetings quickly Build powerful & robust online courses to sell to or instruct people Build communities on Sellful social networking sites with activity feeds, private messaging, & groups See & adjust a visual version of everything going on within your client projects Sync your online shop’s inventory with multiple offline store locations & registers Manage inventory, coupons, & sales through Sellful’s native POS app on your computer Manage employee recruitment, time clocks, payroll & leave requests Automate help desk tasks such as support ticket creation Communicate with your team on multiple chat channels Keep an accounting of your income & expenses Automate billing & website creation for your marketing agency What's in the ERP Agency Plan: White Label Unlimited 10 Sites/Sub Accounts Included 100% White Label For Your Brand Or Your Client's Brand Website Builder Sales Funnel Builder Online Shop Automation Builder CRM & Pipeline Management Email Marketing SMS Marketing Reputation Management 2 Way Communication (Email, SMS & Phone) Appointment Scheduler Memberships Subscriptions Forms, Surveys & Polls Client Portal AI Assistant & Chatbot Social Media Automation Legally Binding Contract Signing Project Management System Online Courses (LMS) Invoicing External CRM Connect Class Attendance & Event Booking Restaurant Builder Support Ticket System Team Chat AliExpress Drop Shipping Accounting Advanced Affiliate Program Community Builder Point Of Sale HR Suite (HR, Time Clock, Payroll & ATS) 5000+ App Integrations 20+ Payment Gateways (No Fees From Us) Custom Mobile App Agency Billing System Setup Wizard Builder Content Cloner Tool Digital Marketing Courses Actionable Marketing PDF Guides Unlimited Contacts Per Site/Sub Account Unlimited Pages Per Site/Sub Account Unlimited Blog Posts Per Site/Sub Account Unlimited Users Per Site/Sub Account Unlimited Products Per Site/Sub Account Unlimited Visitors Per Site/Sub Account 100 Gigs Of File Storage 50,000 Free Email Sends* Unlimited Domain Names Per Site/Sub Account *Email sending can be purchased in packs of 10,000 for $10/Month. You can also add your own external sending service to send without limits. Email sends are shared in a pool throughout all websites and email addresses on the account. System emails are always free. Good to know Length of access: lifetime Redemption deadline: redeem your code within 30 days of purchase Updates included Sellful: ERP Agency Plan (Lifetime) normally costs $1,497 but it can be yours for only $349.97, that's a saving of $1,147.03 (76%) off! For terms, and more details click the link below. Get a lifetime plan to Sellful at 76% off (was $1,497) Although priced in U.S. dollars, this deal is available for digital purchase worldwide. Support queries If you have queries or need support for any of the Neowin Deals, please use the contact form here. Neowin Deals are managed and sold by StackCommerce who represent Neowin on an affiliate basis. Why we post these deals We post these because we earn commission on each sale so as not to rely solely on advertising, which many of our readers block. It all helps toward paying staff reporters, servers and hosting costs. So for those that keep moaning and complaining, be thankful we're still online for you to even do that. Other ways to support Neowin Whitelist Neowin by not blocking our ads Create a free member account to see fewer ads Make a donation to support our day to day running costs Subscribe to Neowin - for $14 a year, or $28 a year for an ad-free experience Disclosure: Neowin benefits from revenue of each sale made through our branded deals site powered by StackCommerce.
    • No its not, there are ton of Youtube videos to get you started, what do you think people did before AI existed?
    • Read this in Humor Simpson 's voice, "Out of my way Moe".
  • Recent Achievements

    • One Year In
      B4dM1k3 earned a badge
      One Year In
    • One Year In
      DarkWun earned a badge
      One Year In
    • Dedicated
      Almohandis earned a badge
      Dedicated
    • Dedicated
      JuvenileDelinquent earned a badge
      Dedicated
    • First Post
      DrWankel earned a badge
      First Post
  • Popular Contributors

    1. 1
      +primortal
      506
    2. 2
      +Edouard
      181
    3. 3
      PsYcHoKiLLa
      86
    4. 4
      Michael Scrip
      78
    5. 5
      Steven P.
      76
  • Tell a friend

    Love Neowin? Tell a friend!