/    Sign up×
Community /Pin to ProfileBookmark

how to use javascript code in php

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

Thanks in advance.

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@deathshadowJun 15.2014 — 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.
Copy linkTweet thisAlerts:
@alexdeefJun 25.2014 — 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>

    &#8226; Both versions must be written into .php files that can be processed by the PHP module.
  • Copy linkTweet thisAlerts:
    @suchetagajdevaJul 09.2014 — 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
    Copy linkTweet thisAlerts:
    @jedaisoulJul 09.2014 — 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.
    <i>
    </i>&lt;?php
    /* some php code*/
    echo '&lt;div id="example"&gt;';
    /* some more php code */


  • b) The php interpreter can use "?>" to drop into HTML mode then "<?php" to return to PHP mode. E.g.:
    <i>
    </i>&lt;?php
    /* some php code*/
    ?&gt;
    &lt;!-- this is html code --&gt;
    &lt;div id="example"&gt;
    &lt;?php
    /* some more php code */


    I hope this helps! ?
    ×

    Success!

    Help @mikestang746 spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 6.17,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @nearjob,
    tipped: article
    amount: 1000 SATS,

    tipper: @meenaratha,
    tipped: article
    amount: 1000 SATS,

    tipper: @meenaratha,
    tipped: article
    amount: 1000 SATS,
    )...