Search
 
SCRIPT & CODE EXAMPLE
 

PHP

using js variable in php

<script type="text/javascript">
var abc= 'this is text';
<?php $abc = "<script>document.write(abc)</script>"?>   
</script>
<?php echo $abc;?>
Comment

javascript php variable

<script type="text/javascript">
   var php_var = "<?php echo $php_var; ?>";
</script>
Comment

js var to php

<script>
   var res = "success";
</script>
<?php
   echo "<script>document.writeln(res);</script>";
?>
Comment

how to use javascript variable in php

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

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

how to use php variable in javascript file

<script type="text/javascript">
// boolean outputs "" if false, "1" if true
var bool = "<?php echo $bool ?>"; 

// numeric value, both with and without quotes
var num = <?php echo $num ?>; // 7
var str_num = "<?php echo $num ?>"; // "7" (a string)

var str = "<?php echo $str ?>"; // "A string here"
</script>
Comment

how to get a variable from php to javascript

<script type="text/javascript">
// boolean outputs "" if false, "1" if true
var bool = "<?php echo $bool ?>"; 

// numeric value, both with and without quotes
var num = <?php echo $num ?>; // 7
var str_num = "<?php echo $num ?>"; // "7" (a string)

var str = "<?php echo $str ?>"; // "A string here"
</script>
Comment

php var use in javascript

$idget=$_GET['id'];
var id = <?php echo json_encode($idget); ?>;
Comment

use php var in js

// (A) CONVERT ARRAY TO STRING
$data = ["Apple", "Banana", "Cherry"];
$encoded = json_encode($data);
?>
 
<!-- (B) THIS IS JAVASCRIPT -->
<script>
// (B1) JSON ENCODED STRING
var encoded = '<?=$encoded?>';
console.log(encoded); // STRING
 
// (B2) JSON PARSE (DECODE)
var data = JSON.parse(encoded);
console.log(data); // ARRAY
Comment

how to use php variable in javascript file

<?php
    $color = "Red";
?>
<script type="text/javascript">var color = "<?= $color ?>";</script>
<script type="text/javascript" src="file.js"></script>
Comment

How can I use a JavaScript variable as a PHP variable?

<script type="text/javascript">
var jvalue = 'this is javascript value';

<?php $abc = "<script>document.write(jvalue)</script>"?>   
</script>
<?php echo  'php_'.$abc;?>
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

how to use php variable in javascript file

alert("color: " + color);
Comment

how to use php variable in javascript file

<?php
$bool = false;
$num = 3 + 4;
$str = "A string here";
?>
Comment

PREVIOUS NEXT
Code Example
Php :: laravel get first record 
Php :: php get text from html 
Php :: get first day of current month php 
Php :: php file for image load 
Php :: update pdo mysql php 
Php :: php echo an array to console 
Php :: laravel blade dump 
Php :: wp get author description 
Php :: add request data in laravel request 
Php :: php import python script 
Php :: php sum array of objects 
Php :: get all artisan commands 
Php :: laravel get current action name 
Php :: laravel remove duplicates from array 
Php :: parametre grouping laravel quert 
Php :: composer create project version 
Php :: how to separate integer from string in php 
Php :: laravel conditional class 
Php :: acf wp_query custom field 
Php :: yii app db createcommand join yii1 
Php :: php randon integer 4 digit 
Php :: laravel old value not working in textarea 
Php :: laravel redirect back url with message 
Php :: merge two arrays one as key to another php 
Php :: PHP time limit (max_execution_time): 
Php :: routing code in drupal 8 
Php :: parameterized function in php 
Php :: Flutter Error - Migrate to android studio - MAC OS 
Php :: how assign default value to laravel migration column 
Php :: select values from mysql using php array of ids 
ADD CONTENT
Topic
Content
Source link
Name
8+6 =