www.webdeveloper.com
Results 1 to 5 of 5

Thread: how to use javascript code in php

  1. #1

    how to use javascript code in php

    I don't know how to use java script in php.

    Thanks in advance.

  2. #2
    Join Date
    May 2014
    Posts
    1,017
    You... don't... PHP executes on the server, JS executes in the browser.

    Or are you asking how to echo out scripting?

    Really, your question doesn't make any sense.
    Java is to JavaScript as Ham is to Hamburger.

  3. #3
    Join Date
    Apr 2014
    Posts
    27
    PHP code is executed on the server side and output data are transmitted to Web browser.
    JavaScript script code is executed by the browser on user's computer.
    Combining these two web programming languages, JavaScript scripts can achieve dynamic results based on data received and processed by the server. Thus, the same web page can contain a JavaScript code for a user and other JS code for another user.

    - There are two ways to combine PHP with JavaScript to achieve a dynamic and personalized result:

    1) By writing the entire JS script within PHP code and adding it to the web page with PHP function "echo" (or "print").
    <?php
    echo '<script type="text/javascript"> // JS code ... </script>';
    ?>
    - The string returned by 'echo' must be a JS code with correct syntax.

    2) By adding in the JS script within the HTML document only the necessary data processed by PHP.
    <script type="text/javascript">
    var var_js = <?php echo $var_php; ?>;
    </script>
    • Both versions must be written into .php files that can be processed by the PHP module.

  4. #4
    Join Date
    Jul 2014
    Posts
    1
    PHP has not changed the state of HTML labels and codes in any capacity, so you can embed a bit of javascript code into a php document precisely the same way you destroy it html record. PHP just permits you to stamp certain parts of a php document as php code which will be interepreted by php program before it is send to the customer program
    Web Development Services

  5. #5
    Join Date
    Mar 2012
    Posts
    1,639
    To clarify what others have said here:

    1. A PHP file is essentially an HTML file with the file extension .PHP instead of .HTML. It is given that extension to tell the web server that it contains PHP code that should be executed before the HTML is passed to the web browser.

    2. A PHP file can contain HTML and JavaScript, that is passed to the browser "as is".

    3. A PHP file can contain HTML and JavaScript that is dynamically generated and passed to the browser.

    4. The HTML and JavaScript is passed in either of two ways:
    a) It can be echo'd directly from the PHP code. E.g.
    Code:
    <?php 
    /* some php code*/
    echo '<div id="example">';
    /* some more php code */
    b) The php interpreter can use "?>" to drop into HTML mode then "<?php" to return to PHP mode. E.g.:
    Code:
    <?php 
    /* some php code*/
    ?>
    <!-- this is html code -->
    <div id="example">
    <?php
    /* some more php code */
    I hope this helps!
    Last edited by jedaisoul; 07-09-2014 at 03:17 AM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles