Search
 
SCRIPT & CODE EXAMPLE
 

PHP

how to add javascript in php

//this is my code 

you can add javascript to your code by using the echo statement and rap all 
your javascript between "" and if there is "" in the javascript code
then add  before any "", and for the php inside rap it with ''
  
  
<?php
  echo "<script>document.write("hello world")</script>"
?>
  
//other example

<?php
  $a = 5;
  echo "<script>document.write('$a')</script>"
?>

//other example
  
<?php 
$TokenData = Auth::user();

echo "<script type="text/javascript">
             localStorage.setItem("id", '$TokenData->id');
             localStorage.setItem("name", '$TokenData->name');
             localStorage.setItem("email", '$TokenData->email');
      </script> ";
Comment

add javascript to functions.php

/****
  To get started, locate and open your functions.php file, 
  then copy and paste the following code snippet:
****/
function ti_custom_javascript() {
    ?>
        <script>
          // your javscript code goes here
        </script>
    <?php
}
add_action('wp_head', 'ti_custom_javascript');

/****
This code above will add JavaScript to your header. 
To apply it to a single post only, you can use the following:
****/
function ti_custom_javascript() {
  if (is_single ('3')) { 
    ?>
        <script type="text/javascript">
          // your javscript code goes here
        </script>
    <?php
  }
}
add_action('wp_head', 'ti_custom_javascript');
/****
Note that you’ll need to replace the 3 
in the above example with the post ID number 
to which you want to add the code. To locate 
that number, navigate to the post from your 
admin dashboard, and then click on Edit. In 
the browser bar’s URL, the ID number is the 
number next to “post=”:
Comment

how to include javascript in php

// simply echo the path anywehre in php you need

echo "<script src="script.js" type="text/javascript"></script>"
Comment

PREVIOUS NEXT
Code Example
Php :: how to remove keys in subarray php 
Php :: how to convert unix timestamp to date php 
Php :: laravel check if string is url 
Php :: laravel multiple group by 
Php :: woocommerce remove payment method when totla is 0 
Php :: laravel is route name 
Php :: check if not empty blade engine 
Php :: laravel validation image or file 
Php :: drupal 8 get enabled languages 
Php :: php foreach alternative syntax 
Php :: php set http status header 
Php :: add custom helper laravel 
Php :: laravel-cors 
Php :: array of dates laravel 
Php :: how to add share icon in wordpress 
Php :: laravel log 
Php :: mysqli_connect php 
Php :: get user by meta wp 
Php :: php is_int 
Php :: redirect back laravel 
Php :: laravel return response view 
Php :: ubuntu install php 
Php :: wordpress get plugin root directory 
Php :: laravel append parameter to links 
Php :: uninstall phpstorm ubuntu 
Php :: Get all Country List using php 
Php :: laravel foreign 
Php :: php var use in javascript 
Php :: wp php get product item attributes name 
Php :: merge collections laravel 
ADD CONTENT
Topic
Content
Source link
Name
6+7 =