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

javascript inside php

<?php
  FONCTION_PHP(){
  ?>
   <script>
    function fonctionJavaScript(){
  }
    </script>
<?php
	}
  ?>
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 :: php cookie 
Php :: how to serve the port in php 
Php :: laravel loop iteration 
Php :: php get object class 
Php :: show only 3 initial letter of month in php 
Php :: get from table laravel 
Php :: laravel migration update table column type 
Php :: redirect from http to https laravel 
Php :: set a minimum character value in php 
Php :: php convert print_r to array 
Php :: php set status code 
Php :: static function model laravel 
Php :: laravel get last id 
Php :: php mail 
Php :: the requested php extension bcmath is missing from your system 
Php :: loop through php array 
Php :: wordpress add meta user 
Php :: Your system folder path does not appear to be set correctly. Please open the following file and correct this: index.php 
Php :: add options page advanced custom fields 
Php :: replace key in php 
Php :: laravel controller update 
Php :: laravel migration table bigint 
Php :: in_array associative array php 
Php :: php bcrypt password verify 
Php :: pretty json php 
Php :: laravel 6 pagination example 
Php :: How to order by using id with firstWhere in laravel 
Php :: load-styles.php 403 
Php :: check date PHP 
Php :: find days with name between two dates in php 
ADD CONTENT
Topic
Content
Source link
Name
7+5 =