Professional Web Applications Themes

Variables accessible by Javascript and PHP - PHP Development

I need a set of variables that I can access in both PHP and in Javascript. There are just a few things I can do in one that I cannot do in the other. I've tried: - URL parameters (i.e. ?a=a&b=b) This works, in fact very well in php, but poorly in Javascript which has no facility to split them. Also, I have more variables than I would like to pass in the URL - Hidden form fields (as described in a recent post) Contrary to the implication of the post, then don't seem to cross pages. For example, if ...

  1. #1

    Default Variables accessible by Javascript and PHP

    I need a set of variables that I can access in both PHP and in Javascript.
    There are just a few things I can do in one that I cannot do in the other.

    I've tried:

    - URL parameters (i.e. ?a=a&b=b)

    This works, in fact very well in php, but poorly in Javascript which has no
    facility to split them. Also, I have more variables than I would like to
    pass in the URL

    - Hidden form fields (as described in a recent post)

    Contrary to the implication of the post, then don't seem to cross pages.
    For example, if page 1 has an <a href> to page 2, page 2 is unaware of the
    hidden form fields on page 1

    Any better suggestions? I'm leaning towards cookies, as both handle them
    reasonably well, but many people turn off cookies.

    Thanks,

    Tom


    Tom Guest

  2. #2

    Default Re: Variables accessible by Javascript and PHP

    Tom Scales wrote: 

    The only guaranteed way of getting information from a page on the user's
    browser into PHP is via GET variables (i.e. part of the URL as above),
    or POST variables using a form.

    The only guaranteed way to get information from PHP onto a page on the
    user's browser is to output it in the file served.

    This includes Javascript. Javascript and PHP can't really "share"
    variables, as they are entirely different technologies. Can you give an
    example of what you need to do?

    --
    Oli
    Oli Guest

  3. #3

    Default Re: Variables accessible by Javascript and PHP

    Tom Scales wrote:
     

    You could use sessions. Put the values into session variables on page 1.
    Then page 2 would need some PHP code to read the sessions vars and generate
    the relevant Javascript code with the values in place.

    --
    +
    srm Guest

  4. #4

    Default Re: Variables accessible by Javascript and PHP


    "srm" <org> wrote in message
    news:com... 
    >
    > You could use sessions. Put the values into session variables on page 1.
    > Then page 2 would need some PHP code to read the sessions vars and
    > generate
    > the relevant Javascript code with the values in place.
    >
    > --
    > +[/ref]

    I thought of that, but Page 1 Javascript needs to set variables that Page 2
    PHP can read (and sometimes vice-versa).

    Tom


    Tom Guest

  5. #5

    Default Re: Variables accessible by Javascript and PHP

    Tom Scales wrote:
     
    >
    > I thought of that, but Page 1 Javascript needs to set variables that Page
    > 2 PHP can read (and sometimes vice-versa).[/ref]

    Starting to look like you've no choice but to use values passed via GET,
    then.

    --
    +
    srm Guest

  6. #6

    Default Re: Variables accessible by Javascript and PHP

    "Passing" variables from PHP to JavaScript is much easier than the other
    way around.

    ie: echo "<script type='text/javascript'>thisPage =
    '{$_SERVER['PHP_SELF']}';</script>";

    (very basic example).. but as soon as you're trying to pass variables
    from JavaScript to PHP it gets quite a bit more complicated. As far as I
    know, you will not be able to do it in any other way other than
    submitting the page with a form (hidden fields and what not) .. or
    through the URL .. (which can reveal data).
    I would say the easiest and best way is to create a form with hidden
    values that javascript sets... and the only way to get to the second
    page (where you need php to get your Js vars) is through javascript form
    submission.... so any link that's on the page would call a js function
    for instance loadPage(url) and that function would verify you have the
    data you need and then submit the form... (form's action attribute
    ofcourse would be the url to your second page.)

    I hope this makes sense to you :)

    take care,

    Paul

    Tom Scales wrote: 
    Paul Guest

  7. #7

    Default Re: Variables accessible by Javascript and PHP

    It does. I ended up using cookies and it works reasonably well. I have to
    force reload the page, though, as the Javascript sets the cookies AFTER PHP
    renders the page, so the reload picks up the new cookie values.

    It works well. It's intended for my photography pages. The thumbnails now
    resize themselves when the window is resized. It's actually kind of cool.

    Tom
    "Paul" <net> wrote in message
    news:uALke.1471779$.. [/ref]


    Tom Guest

  8. #8

    Default Re: Variables accessible by Javascript and PHP

    That's a good way to do it, as long as it works right :)

    You don't have to reload... you can reload without reloading ;) but it
    is quite a bit more complicated... using and dynamically created iframe
    object (with Js) ... if you want to know more about that, just search
    for something like iframe rpc on google...

    peace

    paul

    Tom Scales wrote: [/ref]
    >
    >
    >[/ref]
    Paul Guest

  9. #9

    Default Re: Variables accessible by Javascript and PHP

    OK, will do. The reload works, but I'd rather not do it.

    Tom
    "Paul" <net> wrote in message
    news:v7Wke.1477987$.. 
    >>
    >>[/ref][/ref]

    Tom Guest

  10. #10

    Default Re: Variables accessible by Javascript and PHP

    There is a simple, well known way to transmit data between javascript on a
    page and the server. No page submits or loads, just call a js function.

    Enter XMLHTTPREQUEST and AJAX!

    http://en.wikipedia.org/wiki/AJAX&e=10313
    http://developer.apple.com/internet/webcontent/xmlhttpreq.html

    Paul <net> wrote in
    news:uALke.1471779$6l.530601pd7tw2no:
     [/ref]
    Jam Guest

  11. #11

    Default Re: Variables accessible by Javascript and PHP

    "Jam Pa" <org> wrote in message
    news:243.153.2... 


    I wouldn't say 'well known', since in most cases people just use some
    work-around like a hidden frame or something like that.

    --
    Welcome to Usenet! Please leave tolerance, understanding
    and intelligence at the door. They aren't welcome here.
    eternal piste erection miuku gmail piste com


    Kimmo Guest

  12. #12

    Default Re: Variables accessible by Javascript and PHP

    Jam Pa wrote: [/ref][/ref]

    Hi! Yeah I know about this method too. The reason I decided not to
    mention it... or even use it in my projects yet... is simply because it
    is fairly immature still... and more complicated than the iframe way.
    Iframe's have been supported for quite a while now... so their
    functionality is quite predictable in most cases. I designed an RPC web
    app that had the ability to retreive practically any data required
    without reloading the page... including submitting a form via POST
    without any problems... I have only tested it under IE and Gecko baseb
    browsers though...
    Paul Guest

Similar Threads

  1. How to pass variables from JavaScript to Flex
    By luke14free in forum Macromedia Flex General Discussion
    Replies: 0
    Last Post: May 5th, 08:41 PM
  2. Javascript variables
    By Mario Giannone in forum Coldfusion - Advanced Techniques
    Replies: 4
    Last Post: February 25th, 08:23 PM
  3. Javascript variables to cold fusion
    By hivaluescomputing in forum Coldfusion - Advanced Techniques
    Replies: 1
    Last Post: February 25th, 03:52 AM
  4. Session variables not accessible
    By Holland in forum PHP Development
    Replies: 2
    Last Post: March 2nd, 06:55 PM
  5. Passing variables from ASP to Javascript
    By Gung Ho in forum ASP Components
    Replies: 1
    Last Post: November 28th, 07:26 PM

Bookmarks

Posting Permissions

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139