php & javascript interaction

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • adriann
    New Member
    • Aug 2006
    • 25

    php & javascript interaction

    Hi,

    can PHP be used to execute a JavaScript function?
    And, can JavaScript be used to execute a PHP function?

    if so, what is the basic coding to perform these actions.

    thank you
    Adrian
  • phpmaet
    New Member
    • Sep 2006
    • 27

    #2
    Yes, Let's execute Javascript function in PHP. you can see the following example.

    <title>Untitl ed Document</title>
    <SCRIPT LANGUAGE="javas cript">
    function Test()
    {
    alert ("This is Sample");
    }
    </SCRIPT>
    </head>


    <body>
    <?
    echo "<SCRIPT LANGUAGE='javas cript'>";
    echo "Test();";
    echo "</SCRIPT>";
    ?>


    Thanks

    Comment

    • Banfa
      Recognized Expert Expert
      • Feb 2006
      • 9067

      #3
      I disagree with phpmaet

      php can not execute Javascript, Javascript can not execute php. This is beacuse php runs at the server and javascript runs at the client.

      What phpmaet has done is use php to write javascript which will then run at the client, this might be a slightly subtle distinction but I beleiev it is an important 1.

      I have used this trick myself (having php write javascript) and it is completely legal but php is not running javascript it is writing it for future exection. The javascript, as always, runs at the client computer.

      Comment

      • phpmaet
        New Member
        • Sep 2006
        • 27

        #4
        i also disagree with Banfa

        PHP CAN execute javascript.

        Javascript CAN execute php


        you should cut & paste and run my script.


        sample.php
        ----------

        <head>
        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
        <title>Untitl ed Document</title>
        <SCRIPT LANGUAGE="javas cript">
        document.write( "<? echo 'This is my inside script<BR><BR>' ; ?>");
        function Test()
        {
        alert ("This is Sample");
        }
        function fnClick()
        {
        window.location .href='sample.p hp?c=1';
        }
        </SCRIPT>
        </head>
        <body>
        <?
        if($_GET['c'])
        {
        echo "<SCRIPT LANGUAGE='javas cript'>";
        echo "Test();";
        echo "</SCRIPT>";
        }
        ?>
        <input type="button" name="txs" onclick="fnClic k()" value="Click here" />
        </body>
        </html>

        Comment

        • ronverdonk
          Recognized Expert Specialist
          • Jul 2006
          • 4259

          #5
          Banfa, you are absolutely right!
          What is often forgotten is that PHP can only send HTML (and JS) to the client where it will execute, but not under control of PHP. PHP is merely the echo-er here.

          Ronald :cool:

          Comment

          • adriann
            New Member
            • Aug 2006
            • 25

            #6
            thanks guys. i can now understand the subtle process that are happening here.
            thanks also for the code examples.

            Comment

            • MerilFernando
              New Member
              • Mar 2017
              • 4

              #7
              Same Page Data Interaction

              Code:
              <?php  $xphp= "My PHP WORLD"; ?>   <!-- Define a PHP variable -->
              <script> var xjs='My Java Script world'; </script> <!-- Define a JAVASCRIPT variable -->
              <input type='hidden' id='myhtml' value='My HTML world!' > <!-- Define a HTML variable -->
              
              <BR>sending PHP variable value into JAVASCRIPT <BR>
              <script>
              var xphp='<?php echo $xphp; ?>';
              document.write(xphp);    
              </script>
              
              <BR>getting PHP variable value into HTML <BR>
              <?php echo $xphp; ?>
              
              <BR><BR>getting JAVASCRIPT  variable value into PHP <BR>
              <?php
              $xjs = "<script>document.write(xjs);</script>";
              echo $xjs;
              ?>
              
              <BR>getting JAVASCRIPT  variable value into HTML <BR>
              <script>document.write(xjs);</script>
              
              <BR><BR>getting HTML variable value into JAVASCRIPT  <BR> 
              <script>
              var xhtml=document.getElementById('myhtml').value;
              document.write(xhtml);    
              </script>
              
              <BR>getting HTML variable value into PHP <BR> 
              <?php
              $xhtml = "<script>document.write(document.getElementById('myhtml').value);</script>";
              echo $xhtml;
              ?>

              Comment

              Working...