Javascript and PHP ?

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

    Javascript and PHP ?

    Hi everybody !!

    I am just wondering if Javascript and PHP can interact with each other
    ?

    Let's say i have a variable in Javascript ( call it "var x = 6;")
    Is there anyway that we can use "x" value (which is 6) in PHP code?
    (let's say I want to print "x" out in PHP script)

    In general, can Javascript variables interact with PHP code and vice
    versa ?

    Thank you in advance !!

    PS: My initial thought is this: "It is impossible to do !" Prove me
    wrong ==> :)
  • Chris Hope

    #2
    Re: Javascript and PHP ?

    Jay wrote:
    [color=blue]
    > I am just wondering if Javascript and PHP can interact with each other
    >
    > Let's say i have a variable in Javascript ( call it "var x = 6;")
    > Is there anyway that we can use "x" value (which is 6) in PHP code?
    > (let's say I want to print "x" out in PHP script)
    >
    > In general, can Javascript variables interact with PHP code and vice
    > versa ?[/color]

    Javascript is a client-side technology. PHP is a server-sided technology.
    You can write out Javascript using PHP and you can post stuff in web forms
    and using GET URLs back to the server which can then be processed by PHP.

    In your example, you would need to assign the "x" variable to a hidden form
    value so it's actually posted back to the server, which PHP would then be
    able to use.

    There is no way for PHP to access Javascript variables, other than them
    being submitted in a form, modifying the form's post property or
    redirecting to another page in Javascript and passing the value as part of
    the URL.

    --
    Chris Hope
    The Electric Toolbox - http://www.electrictoolbox.com/

    Comment

    • kingofkolt

      #3
      Re: Javascript and PHP ?

      "Chris Hope" <chris@electric toolbox.com> wrote in message
      news:1083479178 _317@news.athen anews.com...[color=blue]
      > Jay wrote:
      >[color=green]
      > > I am just wondering if Javascript and PHP can interact with each other
      > >
      > > Let's say i have a variable in Javascript ( call it "var x = 6;")
      > > Is there anyway that we can use "x" value (which is 6) in PHP code?
      > > (let's say I want to print "x" out in PHP script)
      > >
      > > In general, can Javascript variables interact with PHP code and vice
      > > versa ?[/color]
      >
      > Javascript is a client-side technology. PHP is a server-sided technology.
      > You can write out Javascript using PHP and you can post stuff in web forms
      > and using GET URLs back to the server which can then be processed by PHP.
      >
      > In your example, you would need to assign the "x" variable to a hidden[/color]
      form[color=blue]
      > value so it's actually posted back to the server, which PHP would then be
      > able to use.
      >
      > There is no way for PHP to access Javascript variables, other than them
      > being submitted in a form, modifying the form's post property or
      > redirecting to another page in Javascript and passing the value as part of
      > the URL.
      >
      > --
      > Chris Hope
      > The Electric Toolbox - http://www.electrictoolbox.com/[/color]

      Then to follow up Jay's question, how do people get information about screen
      resolution, operating system, etc. on visitors to their websites? Everyone
      says it must be done through javascript, but javascript doesn't insert
      information into databases. And if PHP can't access javascript variables,
      then how can you get someone's screen resolution and put it into a database
      without visitors submitting a form?

      - JP


      Comment

      • Chris Hope

        #4
        Re: Javascript and PHP ?

        kingofkolt wrote:
        [color=blue]
        > Then to follow up Jay's question, how do people get information about
        > screen resolution, operating system, etc. on visitors to their websites?
        > Everyone says it must be done through javascript, but javascript doesn't
        > insert information into databases. And if PHP can't access javascript
        > variables, then how can you get someone's screen resolution and put it
        > into a database without visitors submitting a form?[/color]

        That was one thing I forgot. You can use document.write( ) to write out stuff
        into the page as well. You could use this to write out a 1x1 pixel "image"
        which is actually a PHP script that passes in the screen width and height,
        image depth etc. Note that it still needs to be part of the url eg
        image.php?width =1024&height=76 8 (obviously you would construct this url
        using Javascript and the appropriate variables that contain the width and
        height and all the cross browser code you need to make it work correctly).

        For operating system and browser information it's passed as part of the
        user-agent string from the client to the server and is stored in the
        $_SERVER and $_ENV super globals; no need to use Javascript for that.

        Chris

        Comment

        • Raptor

          #5
          Re: Javascript and PHP ?

          Chris Hope wrote:
          or[color=blue]
          > redirecting to another page in Javascript and passing the value as part of
          > the URL.[/color]

          Interesting. I hadn't thought of that, yet. Is creating a
          tiny/invisible new window from Javascript, and submitting form pages
          through it, a valid technique for setting up a vague kind of handshaking
          and client-server transfer? I haven't thought through the limitations
          and capabilities, but it might come in handy sometime.

          --
          --
          Lynn Wallace http://www.xmission.com/~lawall
          "We should not march into Baghdad. ... Assigning young soldiers to
          a fruitless hunt for a securely entrenched dictator and condemning
          them to fight in what would be an unwinnable urban guerilla war, it
          could only plunge that part of the world into ever greater
          instability." George Bush Sr. in his 1998 book "A World Transformed"

          Comment

          • kingofkolt

            #6
            Re: Javascript and PHP ?

            "Chris Hope" <chris@electric toolbox.com> wrote in message
            news:1083543879 _1967@news.athe nanews.com...[color=blue]
            > kingofkolt wrote:
            >[color=green]
            > > Then to follow up Jay's question, how do people get information about
            > > screen resolution, operating system, etc. on visitors to their websites?
            > > Everyone says it must be done through javascript, but javascript doesn't
            > > insert information into databases. And if PHP can't access javascript
            > > variables, then how can you get someone's screen resolution and put it
            > > into a database without visitors submitting a form?[/color]
            >
            > That was one thing I forgot. You can use document.write( ) to write out[/color]
            stuff[color=blue]
            > into the page as well. You could use this to write out a 1x1 pixel "image"
            > which is actually a PHP script that passes in the screen width and height,
            > image depth etc. Note that it still needs to be part of the url eg
            > image.php?width =1024&height=76 8 (obviously you would construct this url
            > using Javascript and the appropriate variables that contain the width and
            > height and all the cross browser code you need to make it work correctly).
            >
            > For operating system and browser information it's passed as part of the
            > user-agent string from the client to the server and is stored in the
            > $_SERVER and $_ENV super globals; no need to use Javascript for that.
            >
            > Chris
            >[/color]

            Ok thanks. I had always heard and seen people collecting screen res. thru
            javascript but I never knew how.

            - JP


            Comment

            • Dariusz

              #7
              Re: Javascript and PHP ?

              In article <5668e569.04050 12200.18b17e8f@ posting.google. com>, artlover70@yaho o.com (Jay) wrote:[color=blue]
              >Hi everybody !!
              >
              >I am just wondering if Javascript and PHP can interact with each other?[/color]

              I had the same problem with a feature I wanted to do with passing a
              variable to a PHP script on a javascript popup and the PHP code to continue
              executing the code in the popup depending on the variables passed to it.

              I could not work out a solution, but after hours of searching (sorry didn't
              bookmark the URL), I did find a solution. Here's the code I have.

              Suppose that you want to call a popup, like I will. The script you want to
              call will call a Javascript popup and pass the variables from the initial
              click to the Javascript, which in turn will go to PHP to execute.

              So suppose you have a page which will call a popup like this:

              <script src="js/pics.js" type="text/javascript">
              <!--
              //-->
              </script>

              <body>
              <a href="#" onclick="go_ope n('doit.php?&ga llery=01&picID= 01');">
              </body>


              This URL will go and run the javascript "pics.js" script which contains
              something like:

              <!--
              function go_open(url)
              {
              window.
              open(url,"Pictu re","width=50,h eight=50,toolba r=no,scrollbars =no,
              status=yes");
              }
              //-->


              Now, at the VERY beginning of the "doit.php" page - you have to write the
              PHP / Javascript page like the following (before the <html> section).

              <?PHP
              // Import the variables from Javascript window.open() method to PHP.

              $G-ID = $_GET['gallery'];
              $P-ID = $_GET['picID'];

              ?>
              //-->
              </script>

              <head><title>Po pup</title></head>
              <body>

              Normal HTML page here mixed with PHP to make use of the vaiables.


              Now later on in the page, you can then call the variables for $G-ID and
              $P-ID as you have imported them using Javascript and PHP.

              Complicated, but it does work.

              Dariusz

              Comment

              Working...