Search
 
SCRIPT & CODE EXAMPLE
 

PHP

php in javascript

If your whole JavaScript code gets processed by PHP, then you can do it just like that.

If you have individual .js files, and you don't want PHP to process them (for example, for caching reasons), then you can just pass variables around in JavaScript.

For example, in your index.php (or wherever you specify your layout), you'd do something like this:

<script type="text/javascript">
    var my_var = <?php echo json_encode($my_var); ?>;
</script>
You could then use my_var in your JavaScript files.

This method also lets you pass other than just simple integer values, as json_encode() also deals with arrays, strings, etc. correctly, serialising them into a format that JavaScript can use.
Comment

use js in php


<?php 
echo '<script type="text/JavaScript"> 
     prompt("GeeksForGeeks");
     </script>'
;
?>
Comment

PREVIOUS NEXT
Code Example
Php :: phpexcel set data type string 
Php :: php uppercase first letter 
Php :: Laravel 5.4 Route back in blade 
Php :: cambiar entre versiones de php linux 
Php :: get last month using php 
Php :: how to make-migrations in laravel 
Php :: PHP Read File modes 
Php :: PHP file reading modes with explaination 
Php :: Sending Data over another website via PHP 
Php :: how to cheeck php 
Php :: store emoji in php 
Php :: check if string starts with php 
Php :: how to get parameter from url in laravel blade 
Php :: PDOException::("SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 1000 bytes") 
Php :: send html email laravel 
Php :: laravel date format 
Php :: laravel delete file after download 
Php :: php array has key 
Php :: define in php 
Php :: nova laravel image 
Php :: convert any phone number in us number format php 
Php :: keep only n elements of array php 
Php :: changing created_at to short date time 
Php :: taxonomy_get_children drupal 8 
Php :: search string inside array of objects php 
Php :: destrroy a session php 
Php :: add custom style to wordpress editor 
Php :: laravel set config 
Php :: wordpress php cpt get all taxonomy 
Php :: laravel auth 6 
ADD CONTENT
Topic
Content
Source link
Name
4+1 =