Bring variables from php to JAVASCRIPT

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

    Bring variables from php to JAVASCRIPT

    Hello,

    Does someone know how can we bring variables from a PHP script to a
    Javascript one ?
    I have to build a Javascript function that needs variables generated by a
    PHP script but i do not know the way to retrieve them into Javascript.

    Many tanks in advance for your help
    Rdgs

    Steph


  • David Dorward

    #2
    Re: Bring variables from php to JAVASCRIPT

    Steph wrote:
    [color=blue]
    > Does someone know how can we bring variables from a PHP script to a
    > Javascript one ?
    > I have to build a Javascript function that needs variables generated by a
    > PHP script but i do not know the way to retrieve them into Javascript.[/color]

    All the PHP runs on the server, then all the JavaScript runs on the client.
    For PHP to send information to JavaScript it has to output JavaScript code.

    <script type="text/javascript">
    var foo = "<?php echo $foo; ?>";
    </script>

    For JavaScript to send information to PHP, it has to get the browser to make
    a new HTTP request.

    document.locati on = "http://www.example.com/myScript.php?fo o=" + foo;

    --
    David Dorward <http://blog.dorward.me .uk/> <http://dorward.me.uk/>
    Home is where the ~/.bashrc is

    Comment

    • Steph

      #3
      Re: Bring variables from php to JAVASCRIPT

      "David Dorward" <dorward@yahoo. com> a écrit dans le message de news:
      cunplj$5do$1$83 02bc10@news.dem on.co.uk...[color=blue]
      > Steph wrote:
      >[color=green]
      >> Does someone know how can we bring variables from a PHP script to a
      >> Javascript one ?
      >> I have to build a Javascript function that needs variables generated by a
      >> PHP script but i do not know the way to retrieve them into Javascript.[/color]
      >
      > All the PHP runs on the server, then all the JavaScript runs on the
      > client.
      > For PHP to send information to JavaScript it has to output JavaScript
      > code.
      >
      > <script type="text/javascript">
      > var foo = "<?php echo $foo; ?>";
      > </script>
      >
      > For JavaScript to send information to PHP, it has to get the browser to
      > make
      > a new HTTP request.
      >
      > document.locati on = "http://www.example.com/myScript.php?fo o=" + foo;
      >
      > --
      > David Dorward <http://blog.dorward.me .uk/> <http://dorward.me.uk/>
      > Home is where the ~/.bashrc is[/color]

      I've made a test and it works.
      Thank you very much !


      Comment

      • Matthew Lock

        #4
        Re: Bring variables from php to JAVASCRIPT

        You might find this project handy for getting Javascript to talk to PHP
        over XmlHttpRequest: http://jpspan.sourceforge.net/wiki/doku.php

        Comment

        • Peter Stickney

          #5
          Re: Bring variables from php to JAVASCRIPT

          If you wanted to save a few keystrokes you could
          var foo = "<?=$foo; ?>"

          Comment

          • David Dorward

            #6
            Re: Bring variables from php to JAVASCRIPT

            Peter Stickney wrote:
            [color=blue]
            > If you wanted to save a few keystrokes you could
            > var foo = "<?=$foo; ?>"[/color]

            Save a few keystroks compared to what? Please quote some context!

            That depends on the PHP short tags option being enabled.

            --
            David Dorward <http://blog.dorward.me .uk/> <http://dorward.me.uk/>
            Home is where the ~/.bashrc is

            Comment

            Working...