use a javascript variable in php?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • mbasil7

    use a javascript variable in php?

    Hi at all!

    I want to use a javascript variable in php. The reason is that i want
    to know the client's screen resolution.
    Here is the code i'm using:

    <head>
    <script language='JavaS cript'>
    <!--
    var a=(screen.width-750)/2;

    function getvariable(val ) {
    var dummy = eval(val);
    document.write( dummy);
    }
    // -->
    </script>
    <?php
    function get_JS_var($js_ var_name) {
    $x = "<script> getvariable('" . $js_var_name . "'); </script>";
    return $x;
    }
    ?>
    </head>
    <body>
    <?php
    $screen_pos = get_JS_var("a") ;
    $b="left:".$scr een_pos."";
    ?>
    <div style="position :absolute; <?print $b;?>; top:0; z-index:4">
    etc....
    </body>

    The problem is that if i output the $b outside the div element, it
    displays to the browser the right result. But inside the div element
    it outputs the
    left:<script> getvariable('a' ); </script> and so the layer apears in
    wrong place.

    Any ideas about this or other way to manage to output the div content
    in a place according to the client's screen resolution?

    Thanks in advance.
  • Andy Hassall

    #2
    Re: use a javascript variable in php?

    On 28 Jun 2005 15:47:34 -0700, mbasil7@gmail.c om (mbasil7) wrote:
    [color=blue]
    >I want to use a javascript variable in php.[/color]

    This is more complicated than it may appear.
    [color=blue]
    >The reason is that i want
    >to know the client's screen resolution.
    >Here is the code i'm using:
    >
    ><head>
    ><script language='JavaS cript'>
    > <!--
    > var a=(screen.width-750)/2;
    >
    > function getvariable(val ) {
    > var dummy = eval(val);
    > document.write( dummy);
    > }
    > // -->
    > </script>
    > <?php
    > function get_JS_var($js_ var_name) {
    > $x = "<script> getvariable('" . $js_var_name . "'); </script>";
    > return $x;[/color]

    What are you expecting this to do?
    [color=blue]
    > }
    >?>
    ></head>
    ><body>
    ><?php
    >$screen_pos = get_JS_var("a") ;
    >$b="left:".$sc reen_pos."";
    >?>
    ><div style="position :absolute; <?print $b;?>; top:0; z-index:4">[/color]

    This isn't using a Javascript variable in PHP, this is outputting invalid CSS
    within HTML (trying to put a <script> tag in a style).

    You seem to be badly confused as to the execution order and execution location
    of PHP versus Javascript.

    In this case it appears you're better off leaving PHP out of the picture and
    just doing this all in client-side Javascript?

    --
    Andy Hassall / <andy@andyh.co. uk> / <http://www.andyh.co.uk >
    <http://www.andyhsoftwa re.co.uk/space> Space: disk usage analysis tool

    Comment

    • Niklas Schönberg

      #3
      Re: use a javascript variable in php?

      mbasil7 wrote:[color=blue]
      > Hi at all!
      >
      > I want to use a javascript variable in php. The reason is that i want
      > to know the client's screen resolution.[/color]

      How about this:

      --- Code ---
      <?php
      if (!isset($width) || !isset($height) )
      echo "<script type='text/javascript'>
      document.locati on='index.php?w idth='+screen.w idth+'&height=' +screen.height;
      </script>";

      echo "$width X $height";

      ?>
      --- End Code ---

      I don't have the ability to test it right now, so don't bash me too much
      if it doesn't work. However the idea should be pretty sound.

      Comment

      • Google Mike

        #4
        Re: use a javascript variable in php?

        Client-side Javascript is interpreted in the browser after the server
        has processed the PHP, so you can't process Javascript in this way
        inside the PHP. There are workarounds, however...

        On your login page of your web app, you could capture the onSubmit
        event in the FORM html tag, then have a function update some hidden
        input vars and then do a return true. That posts this to the next page
        and you can read, with PHP, the values from the hidden input vars. Just
        a couple days ago I wrote an article in comp.lang.php on a timezone
        problem and how I resolved it, and I used this technique with
        Javascript.

        Or, you could have your index.php have some javascript that detects it
        is the first time the user has come to your web page because they don't
        have a persistent cookie that says, for instance, "screensize ". You
        could detect this with Javascript and then write this cookie with
        Javascript. When the following PHP pages in your app run, they can read
        this cookie and process it accordingly. Or, the other option is that
        once this cookie is set, you can tell the browser to refresh back to
        itself again so that the index.php page can immediately pick up the
        cookie and process it again.

        Or, you could use Javascript to tell the page to redirect back to
        itself but pass some query params for screen width and height. You
        could then parse the query params in PHP.

        Comment

        • cyberhorse

          #5
          Re: use a javascript variable in php?

          Didn't the server return browser information in the superglobal
          $_SERVER array?

          Comment

          • mbasil7

            #6
            Re: use a javascript variable in php?

            I have knowledge of them that you wrote. Simply i wonder if there is a
            way to do this without refresh the page or using a cookie.
            Probably, the problem will solve if i used javascript only for my div
            element.

            Thanks all for your responses.

            Comment

            • Niklas Schönberg

              #7
              Re: use a javascript variable in php?

              cyberhorse wrote:[color=blue]
              > Didn't the server return browser information in the superglobal
              > $_SERVER array?[/color]

              Yes there is some browser info, but as far as I know, the screen size is
              not included.

              Comment

              • Niklas Schönberg

                #8
                Re: use a javascript variable in php?

                mbasil7 wrote:[color=blue]
                > I have knowledge of them that you wrote. Simply i wonder if there is a
                > way to do this without refresh the page or using a cookie.
                > Probably, the problem will solve if i used javascript only for my div
                > element.[/color]

                You cannot pass javascript variables to php for the reasons mike stated,
                but if you just want to display the screen size, or resize your div
                element according to the screen size, that is totally possible. To pass
                variables to CSS, you don't need to pass them to php first.

                I just re-read your fisrt post, and probably figured out what you want
                to do. Let's see if we can find a fix for it.

                --- Code ---

                <head>
                <script type='text/javascript' language='JavaS cript'>
                <!--
                function moveDiv(id) {
                d = document.getEle mentById(id);
                a = (screen.width-750)/2;
                d.style.left = a+"px";
                }
                // -->
                </script>
                </head>
                <body onload="moveDiv ('divthingy');" >
                <div id="divthingy" style="position :absolute; left:0; top:0; z-index:4">
                etc....
                </body>

                --- End Code ---

                That should work, again untested. As you see there is no php needed at all.

                -----BEGIN PGP SIGNATURE-----
                Version: GnuPG v1.4.1 (GNU/Linux)
                Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

                iD8DBQFCwoEoZeV xmdI1of8RAlNLAJ 4x0FFs9dKtw5Iq7 nz6HNwOYihFJgCg r73Q
                N+iPa5mE1bfwb32 va1BvfEE=
                =yJp1
                -----END PGP SIGNATURE-----

                Comment

                • Good Man

                  #9
                  Re: use a javascript variable in php?

                  mbasil7@gmail.c om (mbasil7) wrote in news:fdf7cf3e.0 506281447.6ff0f 5a6
                  @posting.google .com:
                  [color=blue]
                  > Hi at all!
                  >
                  > I want to use a javascript variable in php. The reason is that i want
                  > to know the client's screen resolution.[/color]

                  hold up the PHP and go back to HTML/Web design basics.

                  You should NEVER design a site based upon screen resolution. EVER. Learn
                  how to write flexible HTML/XHTML/CSS before embarking on PHP.

                  Comment

                  • ECRIA Public Mail Buffer

                    #10
                    Re: use a javascript variable in php?

                    PHP is a server-side scripting language. This means that it is executed on
                    the web server, and the result of this execution is the HTML that is sent to
                    the client (web browser).

                    Javascript is run on the client machine by the web browser (Firefox, IE,
                    etc).

                    So - you can access PHP variables in your Javascript - or more correctly,
                    you can use PHP to set Javascript variables, but you cannot access a
                    Javascript variable directly in PHP, because the Javascript is just
                    meaningless text until it reaches the web browser, at which point the PHP is
                    all done executing and is no longer in the picture.

                    This said, there IS at least one way for you to access to your Javascript
                    variables (indirectly) from your PHP scripts. It involves the following
                    steps:

                    1. Start a session in your PHP script. Check if the $_SESSION["scriptvars "]
                    array or $_POST array is set, and if it is, that your desired variables are
                    contained within either. If they are in your session, continue with your
                    regular PHP script. If they are in your $_POST array, set your session array
                    with them and continue with your regular PHP script. This will make more
                    sense in a minute.
                    2. If they are not set, then you will need to "fetch" them. This is done by
                    sending a blank html document with a hidden form and an onload=getvars( ) in
                    the <body> tag.
                    3. Write your JavaScript getvars() function to set the values of all the
                    (hidden) fields in your form - such as screen size, window size, etc. When
                    this page loads in the function will be executed. The last line in the
                    function should be document.formna me.submit(); which will submit the form.
                    Use method=POST and target=yourphpf ilename.php.

                    Note that this approach can have an undesirable effect on search engines as
                    they will likely only see the blank page used to fetch the variables
                    available to Javascript.

                    ECRIA
                    Providing a surprisingly human shopping experience. Trusted and secure. Millions of domains to choose from.



                    Comment

                    • mbasil7

                      #11
                      Re: use a javascript variable in php?

                      Thans a lot all of you for your responses. Finally i found the
                      solution for my problem. Simply my first thought was to use a
                      javascript variable in php. But in that way i must refresh the page at
                      least one time.
                      The solution is to use javascript to "echo" the div element only. In
                      this way i can access the javascript variable and output the layer in
                      the right place in the browser window. Here is the code i finally use:
                      <head>
                      ..
                      ..
                      ..
                      <script language="JavaS cript" type="text/JavaScript">
                      <!--
                      var pos;

                      if (window.innerWi dth)
                      {
                      pos =window.innerWi dth;
                      }
                      else if (document.docum entElement &&
                      document.docume ntElement.clien tWidth)
                      {
                      pos= document.docume ntElement.clien tWidth;
                      }
                      else if (document.body)
                      {
                      pos= document.body.c lientWidth;
                      }
                      //-->
                      </script>
                      ..
                      ..
                      </head>

                      <body>
                      ..
                      ..
                      ..
                      <script language="JavaS cript" type="text/JavaScript">
                      <!--
                      if (pos>750) {
                      document.write ('<div id="html" style="position :absolute;
                      left:'+(((pos-750))/2+293)+'; top:19; z-index:4;
                      visibility:hidd en;">');
                      } else {
                      document.write ('<div id="html" style="position :absolute; left:293;
                      top:19; z-index:4; visibility:hidd en;">');
                      }
                      //-->
                      </script>
                      ..
                      ..
                      ..
                      </body>
                      </html>


                      I specify that the javascript code in the head section gives to the
                      pos the size of the browser window.

                      Comment

                      • Jason Hodges

                        #12
                        Re: use a javascript variable in php?

                        AMEN!
                        -J


                        Good Man wrote:[color=blue]
                        > mbasil7@gmail.c om (mbasil7) wrote in news:fdf7cf3e.0 506281447.6ff0f 5a6
                        > @posting.google .com:
                        >
                        >[color=green]
                        >>Hi at all!
                        >>
                        >>I want to use a javascript variable in php. The reason is that i want
                        >>to know the client's screen resolution.[/color]
                        >
                        >
                        > hold up the PHP and go back to HTML/Web design basics.
                        >
                        > You should NEVER design a site based upon screen resolution. EVER. Learn
                        > how to write flexible HTML/XHTML/CSS before embarking on PHP.[/color]


                        --
                        _______________ ____
                        Jay

                        Comment

                        • scott Johnson

                          #13
                          Re: use a javascript variable in php?

                          But if you wanted to import a javascript variable, you can put it in a
                          form as a hidden field and submit it to the process php page and then
                          access it by $_POST[].

                          There may be an easier way, but that is how I have done in in the past.
                          I have yet to find a need for it in any of my current projects, so this
                          method may be obsolete.

                          But like Jason said, nothing screams 'amateur' then a site that says
                          "Best viewed in...."

                          Jason Hodges wrote:[color=blue]
                          > AMEN!
                          > -J
                          >
                          >
                          > Good Man wrote:
                          >[color=green]
                          >> mbasil7@gmail.c om (mbasil7) wrote in news:fdf7cf3e.0 506281447.6ff0f 5a6
                          >> @posting.google .com:
                          >>
                          >>[color=darkred]
                          >>> Hi at all!
                          >>>
                          >>> I want to use a javascript variable in php. The reason is that i want
                          >>> to know the client's screen resolution.[/color]
                          >>
                          >>
                          >>
                          >> hold up the PHP and go back to HTML/Web design basics.
                          >> You should NEVER design a site based upon screen resolution. EVER.
                          >> Learn how to write flexible HTML/XHTML/CSS before embarking on PHP.[/color]
                          >
                          >
                          >[/color]

                          --
                          Scott Johnson

                          Comment

                          Working...