Java Script and PHP

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

    #1

    Java Script and PHP

    I have an application that uses Google Maps. This works fine but I ned
    to read a file. I can do this in PHP but how to mix PHP with Java
    Script? Do they live together or do I need to end each block of script
    then start the php - then end it etc.

    regards


    Hardy
  • macca

    #2
    Re: Java Script and PHP

    PHP is server side and is converted to HTML by the time it reaches the
    client (browser). JavaScript is client side and is interpreted by the
    client (browser) on the other side.

    Thus, you can write javascript with PHP but you cant write PHP with
    javascript.

    e.g.

    <?php

    echo '<script type="text/javascript">ale rt("javascript alert")</
    script>';


    ?>

    Comment

    • puzz

      #3
      Re: Java Script and PHP

      macca wrote:
      PHP is server side and is converted to HTML by the time it reaches the
      client (browser). JavaScript is client side and is interpreted by the
      client (browser) on the other side.
      >
      Thus, you can write javascript with PHP but you cant write PHP with
      javascript.
      >
      e.g.
      >
      <?php
      >
      echo '<script type="text/javascript">ale rt("javascript alert")</
      script>';
      >
      >
      ?>
      Or save a file named myscript.js.php . In the file you can use both
      javascript and php. For example, you can write:

      alert( <?php 12 + 15 ?)

      and then include it in your html with:

      <script type="text/javascript" language="JavaS cript"
      src="myscript.j s.php"></script>

      --
      www.panoye.com :: virtual tour

      Comment

      • Jonas Werres

        #4
        Re: Java Script and PHP

        Or save a file named myscript.js.php . In the file you can use both
        javascript and php. For example, you can write:
        >
        alert( <?php 12 + 15 ?)
        >
        and then include it in your html with:
        >
        <script type="text/javascript" language="JavaS cript"
        src="myscript.j s.php"></script>
        >
        --
        www.panoye.com :: virtual tour
        This will be send with the wrong Header (normally text/html). You have
        to add
        header('Content-type: application/x-javascript');

        Comment

        • macca

          #5
          Re: Java Script and PHP

          On Dec 23, 1:43 pm, Jonas Werres <jo...@example. orgwrote:
          Or save a file named myscript.js.php . In the file you can use both
          javascript and php. For example, you can write:
          >
          alert( <?php 12 + 15 ?)
          >
          and then include it in your html with:
          >
          <script type="text/javascript" language="JavaS cript"
          src="myscript.j s.php"></script>

          Its still the same thing. The PHP is interpreted on the server before
          the javascript on the client,

          so, what is sent to the client is actually

          alert(27)

          it's the same as having a file called script.php and writing

          <script language="javas cript" type="text/javascript">
          alert(<?php echo 12 + 15; ?>)
          </script>

          Comment

          Working...