Results 1 to 13 of 13

Thread: Javascript Variable into $_Session Variable

  1. #1
    Join Date
    Feb 2005

    Javascript Variable into $_Session Variable

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

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

  2. #2
    Join Date
    Nov 2009
    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
     var yourvar = "ham";
     location.href = "yourphp.php?yourvar="+yourvar;
     $_SESSION['yourvar'] = $_GET['yourvar'];
    A better way would be ajax, but basically it works the same way.

  3. #3
    Join Date
    Aug 2007
    New Jersey, US
     var yourvar = "ham";
     location.href = "yourphp.php?yourvar="+yourvar;
     $_SESSION['yourvar'] = $_GET['yourvar'];
    Hey Specht08,

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

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

  4. #4
    Join Date
    Nov 2009
    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
    New Jersey, US
    Oh - maybe I shoud've read the post title. That makes sense now.


  6. #6
    Join Date
    Feb 2005
    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
    Falls Church, Va.
    And if the user has Javascript disabled in their browser?

  8. #8
    Join Date
    Nov 2009
    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
    Falls Church, Va.
    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.


  10. #10
    Join Date
    Jun 2009
    Washington (State)
    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.


  11. #11
    Join Date
    Nov 2009
    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
    Washington (State)
    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.


  13. #13
    Join Date
    Nov 2009
    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



X vBulletin 4.2.2 Debug Information

  • Page Generation 0.10977 seconds
  • Memory Usage 2,967KB
  • Queries Executed 13 (?)
More Information
Template Usage (35):
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_global_above_footer
  • (1)ad_global_below_navbar
  • (1)ad_global_header1
  • (1)ad_global_header2
  • (1)ad_navbar_below
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (1)ad_thread_first_post_content
  • (1)ad_thread_last_post_content
  • (4)bbcode_code
  • (1)bbcode_php
  • (4)bbcode_quote
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)headinclude_bottom
  • (13)memberaction_dropdown
  • (1)navbar
  • (4)navbar_link
  • (1)navbar_moderation
  • (1)navbar_noticebit
  • (1)navbar_tabs
  • (2)option
  • (13)postbit
  • (13)postbit_onlinestatus
  • (13)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available (6):
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files (26):
  • ./showthread.php
  • ./global.php
  • ./includes/class_bootstrap.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/functions_navigation.php
  • ./includes/class_friendly_url.php
  • ./includes/class_hook.php
  • ./includes/class_bootstrap_framework.php
  • ./vb/vb.php
  • ./vb/phrase.php
  • ./includes/functions_facebook.php
  • ./includes/functions_calendar.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_notice.php
  • ./packages/vbattach/attach.php
  • ./vb/types.php
  • ./vb/cache.php
  • ./vb/cache/db.php
  • ./vb/cache/observer/db.php
  • ./vb/cache/observer.php 

Hooks Called (70):
  • init_startup
  • friendlyurl_resolve_class
  • init_startup_session_setup_start
  • database_pre_fetch_array
  • database_post_fetch_array
  • init_startup_session_setup_complete
  • global_bootstrap_init_start
  • global_bootstrap_init_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • load_show_variables
  • load_forum_show_variables
  • global_state_check
  • global_bootstrap_complete
  • global_start
  • style_fetch
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • strip_bbcode
  • friendlyurl_clean_fragment
  • friendlyurl_geturl
  • forumjump
  • cache_templates
  • cache_templates_process
  • template_register_var
  • template_render_output
  • fetch_template_start
  • fetch_template_complete
  • parse_templates
  • fetch_musername
  • notices_check_start
  • notices_noticebit
  • process_templates_complete
  • friendlyurl_redirect_canonical
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • memberaction_dropdown
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • build_navigation_data
  • build_navigation_array
  • check_navigation_permission
  • process_navigation_links_start
  • process_navigation_links_complete
  • set_navigation_menu_element
  • build_navigation_menudata
  • build_navigation_listdata
  • build_navigation_list
  • set_navigation_tab_main
  • set_navigation_tab_fallback
  • navigation_tab_complete
  • fb_like_button
  • showthread_complete
  • page_templates