How to run JavaScript from PHP?
Last Updated :
31 Jul, 2021
JavaScript is the client side scripting language and PHP is the server side scripting language. JavaScript is used as client side to check and verify client details and PHP is server side used to interact with database. In PHP, HTML is used as a string in the code. In order to render it to the browser, we produce JavaScript code as a string in the PHP code.
Example 1: Write JavaScript code within PHP code
<?php
echo '<script type= "text/JavaScript" >
prompt( "GeeksForGeeks" );
</script>'
;
?>
|
Output:
Example 2: Write JavaScript code outside of PHP code (in same PHP file)
<?php
?>
<script type= "text/javascript" >
alert( 'GeeksforGeeks!' );
</script>
|
Output:
Example 3: JavaScript Function – DOM Manipulation (in same PHP file)
<?php
echo "<div id='demo'></div>" ;
?>
<script type= "text/JavaScript" >
var x = myFunction(11, 10);
document.getElementById( "demo" ).innerHTML = x;
function myFunction(a, b) {
return a * b;
}
</script>
|
Output:
110
JavaScript is best known for web page development but it is also used in a variety of non-browser environments. You can learn JavaScript from the ground up by following this JavaScript Tutorial and JavaScript Examples.
PHP is a server-side scripting language designed specifically for web development. You can learn PHP from the ground up by following this PHP Tutorial and PHP Examples.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...