Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to incorporate javascript in php?

Tags:

javascript

php

This is the way I am currently doing it.

 <?php
//show footer
echo "<script type='text/javascript'>\n";
echo "alert('Congrats');\n";
echo "</script>";

?>

Is there a better way than just to echo it?

like image 401
ggfan Avatar asked Dec 05 '22 03:12

ggfan


1 Answers

Just put your JavaScript code outside PHP tags:

<?php
  // your PHP code goes here
?>
// your javascript function out of the PHP tag.
function f() {
   alert('congrats');
}
like image 162
Pablo Santa Cruz Avatar answered Dec 09 '22 15:12

Pablo Santa Cruz