www.webdeveloper.com
Results 1 to 13 of 13

Thread: Javascript Variable into $_Session Variable

  1. #1
    Join Date
    Feb 2005
    Posts
    97

    Javascript Variable into $_Session Variable

    I am trying to make my javascript variable a PHP $_SESSION variable.

    Code:
    <script>
    var myvariable = "something"
    "[<?php $_SESSION['PHPVariable'] ?>" = myvariable </script>
    I'm sure it's something stupid.. please help.

  2. #2
    Join Date
    Nov 2009
    Posts
    44
    PHP = server side
    Javascript = client side

    You can only write a javascript variable into a session if you pass the variable on with get or post params.

    PHP would look like this
    Code:
    Javascript:
     var yourvar = "ham";
     location.href = "yourphp.php?yourvar="+yourvar;
    PHP:
     session_start();
     $_SESSION['yourvar'] = $_GET['yourvar'];
    A better way would be ajax, but basically it works the same way.

  3. #3
    Join Date
    Aug 2007
    Location
    New Jersey, US
    Posts
    1,779
    Code:
    Javascript:
     var yourvar = "ham";
     location.href = "yourphp.php?yourvar="+yourvar;
    PHP:
     session_start();
     $_SESSION['yourvar'] = $_GET['yourvar'];
    Hey Specht08,

    Just a side question. What's the advantage of using sessions for this? I would normally just do:

    Code:
    $newvar = $_GET['yourvar'];
    Is there anything wrong with that?

  4. #4
    Join Date
    Nov 2009
    Posts
    44
    If you just want to store it in an ordinary variable then yes.

    But the question was:
    I am trying to make my javascript variable a PHP $_SESSION variable.
    So we need a session

  5. #5
    Join Date
    Aug 2007
    Location
    New Jersey, US
    Posts
    1,779
    Oh - maybe I shoud've read the post title. That makes sense now.

    Thanks.

  6. #6
    Join Date
    Feb 2005
    Posts
    97
    Thanks Specht08!! It worked great!!!

    I've been trying to grab querystring variables via $_GET but bc I didn't submit my form, had to go to javascript... (very roundabout way of getting the results I need.. )

    A better way would be ajax, but basically it works the same way.
    How so?

    Thanks again!!

  7. #7
    Join Date
    Jul 2009
    Location
    Falls Church, Va.
    Posts
    780
    And if the user has Javascript disabled in their browser?

  8. #8
    Join Date
    Nov 2009
    Posts
    44
    Than it wouldn't work. But I don't know a lot of people who disable Javascript.
    I don't see this as a real problem. And if the user does it is his own fault.

  9. #9
    Join Date
    Jul 2009
    Location
    Falls Church, Va.
    Posts
    780
    I completely understand ya, honestly.

    Please note the rhetorical question that follows is not intended to be patronizing in any way and has nothing to do with the posted solution. I happen to develop for the government and was taught to always at least consider a way to gracefully degrade site functionality if client side scripting is disabled. This doesn't mean I can work around it 100&#37; of the time, it simply means I consider this during the design phase. Much like Section 508 compliancy for accessibility has best practice recommendations, actually one of them happens to be about no reliance on client side scripting. In the real world, we all know perfection is unattainable. But with that said, here is my question:

    Have you considered if its possible to use PHP to generate that Javascript variable in question? Meaning, if the value comes from a database, cookie or even passed via $_SESSION in code prior to the point you require it now? Ask yourself if earlier in the code, or on some previous page, you could work around the issue without too much effort, so you'd not have to rely on Javascript to populate that variable. This is a rhetorical question, meaning please ponder and no need to answer. It is very likely you need to do exactly what you asked when it's all said and done, of course, and I don't know enough of your project to suggest otherwise.

    This is an intellectual exercise just to get you thinking about the original design.

    No need for you or anyone to reply, but comments welcomed if you feel the need.

    -jim

  10. #10
    Join Date
    Jun 2009
    Location
    Washington (State)
    Posts
    58
    Why not set a cookie? Use javascript to set a cookie: (document.cookie = "javascriptVar=my&#37;20string"; )

    Then within php:
    PHP Code:
    if isset($_COOKIE['javascriptVar']) $_SESSION['javascriptVar'] = $_COOKIE['javascriptVar']; 
    The advantage of this is that there is no "get" stuff in the url.

    --Dave

  11. #11
    Join Date
    Nov 2009
    Posts
    44
    that doesn't make it easier. You would also need to load a php file, in this case without a get param. And additionally I need to set a cookie in javascript.

    And as is said:
    A better way would be ajax, but basically it works the same way.
    In this case there is no "get stuff" in the url as well.

  12. #12
    Join Date
    Jun 2009
    Location
    Washington (State)
    Posts
    58
    Ajax calls are basic html calls. Browsers send cookies with them. You can set a cookie, make a totally unrelated AJAX call, and the cookie goes with it. Or, you can add the javascript variable to the unrelated AJAX call. It seems to me that creating a separate AJAX call just for this purpose is a a bit of a waste.

    -Dave

  13. #13
    Join Date
    Nov 2009
    Posts
    44
    I never heard that before that ajax uses cookies but I will study the way ajax functions.
    In order to get rid of your
    "get" stuff
    we can write the variable into a hidden text field and submit the form to send the the data via post. So we don't need cookies and our url looks fancy again.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles