Search
 
SCRIPT & CODE EXAMPLE
 

PHP

php pass a variabele to js

<script>
'var name = <?php echo json_encode($name); ?>;
</script>'
Comment

how to use javascript variable in php

<script>
var p1 = "success";
</script>

<?php
echo "<script>document.writeln(p1);</script>";
?>
Comment

how to add javascript to a php variable

//this is my code 

$value = <<<data
       <!--instead of writing data we can name it anything 
       as long as we close it with the same name, 
       and that name must not be a keyword in javascript -->

        <script>
         document.write("hello world");
        </script>
       data;

so the php variable $value will not store the string "hello world" inside it,
so the php variable is just a temporary container for the javascript code and nothing else,
and if we echo the vairable like this:-
  
echo $value;
then hello world will be printed in the browser page,
and it is the same as using echo to excute a javascript code, so it is as:-
  
echo "<script>document.write("hello world")</script>"
  
  
  
//using console instead of document.write
  
$value = <<<inConsole
        <script>
         console.log("hello world");
        </script>
       inConsole;


but the value will not be printed in the console unless we echo the variable, 
like this:-

echo $value
then hello world will be printed in the console
  
  
  
//other examples such as storing data to localstorage
  
$local = <<<localstorage
        <script>
         localStorage.setItem("anything", "hello");
        </script>
        localstorage;


but this code will not work until we echo the variable such as:-
echo $local

and if we open the console and write localstorage then the item anything
with its value hello will appear
  
  
  
  
  
  
and if we want to remove the item from localstorage then use this code:-

$local = <<<localstorage
        <script>
         localStorage.removeItem("anything");
        </script>
        localstorage;

        echo $local;

note: never forget to use the echo statement or the code will not work
  
  
  
  
and if we want to get data from localstorage, we use this code

$local = <<<localstorage
        <script>
         let a = localStorage.getItem("anything");
         document.write(a);
        </script>
        localstorage;

        echo $local;

then the value of the varaible a will be print in the webpage, 
Comment

PREVIOUS NEXT
Code Example
Php :: auto complete order paid2 
Php :: int to string in php 
Php :: php raw array without foreach 
Php :: WordPress Creating “startupl” folder and Wrtting to .htaccess 
Php :: create random username and password php 
Php :: php count avec un tableau bidimentionnel 
Php :: php print array key and value 
Php :: Laravel 9 Multiple File Upload 
Php :: php xpath get all tags under a tag 
Php :: laravel collection modelKeys 
Php :: Registering a variable with $_SESSION. 
Php :: iis change php fastcgi user 
Php :: identify the php function used to get values submitted through a form without using any database? 
Php :: laravel many to many update all pivot 
Php :: auth guard (admin) is not defined laravel 8 
Php :: expresions 
Php :: Comment désactiver la barre latérale Widgets sur des pages spécifiques WordPress 
Php :: Laravel You may use the sectionMissing directive to determine if a section does not have content: 
Php :: wc php after login redirect page 
Php :: php ?: 
Php :: php code for fetching data from database 
Php :: laravel model bind with route in model 
Php :: must return a relationship instance laravel 
Php :: phpunit/phpunit[6.0.0, ..., 6.5.14] require php ^7.0 - your php version (8.0.0) does not satisfy that requirement. 
Php :: how to make a timer in php 
Php :: add column to matrix php 
Java :: android.support.design.widget.coordinatorlayout androidx 
Java :: copy to clipboard java 
Java :: how to check if recyclerview is empty 
Java :: main code of java 
ADD CONTENT
Topic
Content
Source link
Name
3+7 =