Using PHP withing JS

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

    Using PHP withing JS

    Hi. Is it possible to use PHP code within a javascript funtion? For
    example, is the following allowed...

    foo( <?php $_SERVER['QUERY_STRING'] ?>);

    function foo {
    ... does something with string
    }

    Thanks in advance.

    Ziggi


  • Justin Koivisto

    #2
    Re: Using PHP withing JS

    Ziggi wrote:
    [color=blue]
    > Hi. Is it possible to use PHP code within a javascript funtion? For
    > example, is the following allowed...
    >
    > foo( <?php $_SERVER['QUERY_STRING'] ?>);
    >
    > function foo {
    > ... does something with string
    > }[/color]

    Yes, here's an example:

    test.php:
    ---------
    <html>
    <head>
    <title>PHP/JS Test</title>
    <script type="text/javascript">
    function foo($x){
    document.write( '<font color="red"><b> '
    +$x+
    '</b></font> was passed to this function.');
    return true;
    }
    </script>
    </head>
    <body>
    <script type="text/javascript">
    foo('<?php echo $_SERVER['REQUEST_URI'] ?>');
    </script>
    </body>
    </html>

    --
    Justin Koivisto - spam@koivi.com
    PHP POSTERS: Please use comp.lang.php for PHP related questions,
    alt.php* groups are not recommended.

    Comment

    • Andy Hassall

      #3
      Re: Using PHP withing JS

      On Mon, 15 Sep 2003 22:46:50 +0100, "Ziggi" <one_ziggi@hotm ail.com> wrote:
      [color=blue]
      >Hi. Is it possible to use PHP code within a javascript funtion? For
      >example, is the following allowed...
      >
      >foo( <?php $_SERVER['QUERY_STRING'] ?>);
      >
      >function foo {
      > ... does something with string
      >}
      >
      >Thanks in advance.[/color]

      Yes, but bear in mind that you're just getting PHP to output data that's
      inserted literally inside the Javascript.

      You can't go on and call a PHP function from a client-side Javascript event.

      --
      Andy Hassall (andy@andyh.co. uk) icq(5747695) (http://www.andyh.co.uk)
      Space: disk usage analysis tool (http://www.andyhsoftware.co.uk/space)

      Comment

      • Ziggi

        #4
        Re: Using PHP withing JS

        Hmm, this code doesn't seem to work. I corrected the mistake in the line
        foo('<?php echo $_SERVER['REQUEST_URI'] ?>'), ie escaping the single
        quotation marks, but all i got was the line "was passed to this function."
        almost as if the $_SERVER['REQUEST_URI'] has not been set... is this a
        problem with my server?

        Ziggi

        "Justin Koivisto" <spam@koivi.com > wrote in message
        news:lyq9b.669$ ow5.29938@news7 .onvoy.net...[color=blue]
        > Ziggi wrote:
        >[color=green]
        > > Hi. Is it possible to use PHP code within a javascript funtion? For
        > > example, is the following allowed...
        > >
        > > foo( <?php $_SERVER['QUERY_STRING'] ?>);
        > >
        > > function foo {
        > > ... does something with string
        > > }[/color]
        >
        > Yes, here's an example:
        >
        > test.php:
        > ---------
        > <html>
        > <head>
        > <title>PHP/JS Test</title>
        > <script type="text/javascript">
        > function foo($x){
        > document.write( '<font color="red"><b> '
        > +$x+
        > '</b></font> was passed to this function.');
        > return true;
        > }
        > </script>
        > </head>
        > <body>
        > <script type="text/javascript">
        > foo('<?php echo $_SERVER['REQUEST_URI'] ?>');
        > </script>
        > </body>
        > </html>
        >
        > --
        > Justin Koivisto - spam@koivi.com
        > PHP POSTERS: Please use comp.lang.php for PHP related questions,
        > alt.php* groups are not recommended.
        >[/color]


        Comment

        • Justin Koivisto

          #5
          Re: Using PHP withing JS

          Ziggi wrote:
          [color=blue]
          > "Justin Koivisto" <spam@koivi.com > wrote in message
          > news:lyq9b.669$ ow5.29938@news7 .onvoy.net...
          >[color=green]
          >>Ziggi wrote:
          >>
          >>[color=darkred]
          >>>Hi. Is it possible to use PHP code within a javascript funtion? For
          >>>example, is the following allowed...
          >>>
          >>>foo( <?php $_SERVER['QUERY_STRING'] ?>);
          >>>
          >>>function foo {
          >>> ... does something with string
          >>>}[/color]
          >>
          >>Yes, here's an example:
          >>
          >>test.php:
          >>---------
          >><html>
          >><head>
          >><title>PHP/JS Test</title>
          >><script type="text/javascript">
          >>function foo($x){
          >> document.write( '<font color="red"><b> '
          >> +$x+
          >> '</b></font> was passed to this function.');
          >> return true;
          >>}
          >></script>
          >></head>
          >><body>
          >><script type="text/javascript">
          >>foo('<?php echo $_SERVER['REQUEST_URI'] ?>');
          >></script>
          >></body>
          >></html>[/color][/color]
          [color=blue]
          > Hmm, this code doesn't seem to work. I corrected the mistake in the line
          > foo('<?php echo $_SERVER['REQUEST_URI'] ?>'), ie escaping the single
          > quotation marks, but all i got was the line "was passed to this[/color]
          function."[color=blue]
          > almost as if the $_SERVER['REQUEST_URI'] has not been set... is this a
          > problem with my server?
          >[/color]

          Well, there was no mistake in the code, I copied and pasted what I
          posted and it worked fine. However, you will need to have PHP 4.1 or
          greater to use the predefined $_SERVER array. Otherwise use
          $HTTP_SERVER_VA RS instead.

          --
          Justin Koivisto - spam@koivi.com
          PHP POSTERS: Please use comp.lang.php for PHP related questions,
          alt.php* groups are not recommended.

          Comment

          Working...