“Comment écrire PHP dans le fichier javascript” Réponses codées

Comment écrire PHP dans le fichier javascript

<?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.
Emad Zedan

Comment écrire JavaScript dans 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> ";
Wide-eyed Wryneck

Écrire PHP dans JavaScript

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

Réponses similaires à “Comment écrire PHP dans le fichier javascript”

Questions similaires à “Comment écrire PHP dans le fichier javascript”

Plus de réponses similaires à “Comment écrire PHP dans le fichier javascript” dans PHP

Parcourir les réponses de code populaires par langue

Parcourir d'autres langages de code