Search
 
SCRIPT & CODE EXAMPLE
 

PHP

how to write php in javascript file

<?php
$abc = "Abc";
$number = 052;
$mydata = array("val1","val2","val3");
?>
<script type="text/javascript">
var abc = '<?php echo $abc?>';
var number = '<?php echo $number ?>';
var mydata = <?php echo json_encode($mydata); ?>;
</script>
  
After use it directly in your js file wherever you want to use.

abc.js

function test(){
   alert('Print php variable : '+ abc +', '+number); 
}

function printArrVar(){
  alert('print php array variable : '+ JSON.stringify(mydata)); 
}

Beside If you have any critical data which you don't want to show publicly then you can encrypt data with js. and also get original value of it's from js so no one can get it's original value. if they show into publicly.

this is the standard way you can call php script data into js file without including js into php code or php script into js.
Comment

how to write javascript inside 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

write php in javascript

<script type="text/javascript">
	var myString = '<?php echo 1+1; ?>';
</script>
Comment

PREVIOUS NEXT
Code Example
Php :: remove first character from string laravel 
Php :: newline in php 
Php :: store as real file name laravel uplaod 
Php :: laravel get file contents from storage 
Php :: php header location not working 
Php :: php date strtotime format 
Php :: php prime numbers 
Php :: create user with tinker php laravel 
Php :: convert object to array in php 
Php :: php foreach array 
Php :: add field to many to many relationship laravel 
Php :: laravel check if field has changed 
Php :: Invalid argument supplied for foreach() 
Php :: wp get post author link 
Php :: php to int 
Php :: fill zero on php 
Php :: custom post type 
Php :: format date in laravel using carbon 
Php :: wp+get custom field phpto 
Php :: php remove element from array 
Php :: Laravel 9 Clear Cache of Route, View, Config, Event Commands 
Php :: foreignid in laravel 
Php :: sort multidimensional array php by key 
Php :: PHP executable not found. Install PHP and add it to your PATH or set the php.executablePath setting in linux 
Php :: How to display image from aws s3 in laravel blade 
Php :: php http_build_query 
Php :: how to find this day is holiday in php day 
Php :: php get ip from host 
Php :: remove last character from string in php 
Php :: wordpress thumbnail 
ADD CONTENT
Topic
Content
Source link
Name
9+6 =