How to use a variable from php file to javascript file - javascript

How to use a variable from php file to javascript file
in php file I assigned the $phpVariable to jsVariable with this code:
<script>
var jsVariable = <?php echo $phpVariable; ?>;
</script>
in Javascript file: I test the jsVariable with this code:
<script>
alert( jsVariable );
</script>
but the output is ":undefined"
Thanks
Moi

If such variable is a string, you should wrap it in quotes:
<script>
var jsVariable = "<?php echo $phpVariable; ?>";
</script>
EDIT but you should never do that. You'd better inject it as a data-* attribute of a DOM element, or either fetch it from a JSON config file / string you'll be parsing, like:
<script type="application/json" id="json-config">
<?php echo json_encode($someObject); ?>
</script>
And then:
var config = $.parseJSON( $('#json-config').html() )

<?php
print "<script>";
print "var jsVariable = $phpVariable";
print "</script>";
?>

Related

Problems in passing PHP array to JavaScript

I understand that the format for passing a PHP array to Javascript is:
<script type="text/javascript">
var obj = <?php echo json_encode($php_variable); ?>;
</script>
I have a php function that stores some values in a longitude and latitude array. These array do hold the right values within php since print_r() within php shows me that the array is correct.
print_r($latitude_array);
print_r($longitude_array);
Now, I pass on this array to JS in this manner:
<script>
var lati_array = "<?php echo json_encode($latitude_array); ?>";
var longi_array = "<?php echo json_encode($longitude_array); ?>";
alert(lati_array[0]);
</script>
In the beginning, when I open the HTML file, it shows me an empty array (which is expected because the PHP arrays aren't filled yet). Then user enters something, the php arrays are filled up with longitudes and latitudes. These values should now be passed to JS. However, it doesn't alert anything after that. I can't be sure if array is successfully passed to JS. What am I missing?
Try this:
<script>
var data = <?php echo json_encode( $data ); ?>;
</script>
Try like below:
<?php
$array_var = array(111, 222, 333, 444);
?>
<script>
var array_var = "<?php echo json_encode($array_var); ?>";
console.log(array_var);
array_var = JSON.parse(array_var);
console.log(array_var);
alert(array_var[0]);
</script>
You are getting a string in lati_array , Try to convert it into json like this:
<script>
var lati_array = "<?php echo !empty($latitude_array) ? json_encode($latitude_array) : ''; ?>";
var longi_array = "<?php echo !empty($longitude_array) ? json_encode($longitude_array) : ''; ?>";
lati_array = JSON.parse(lati_array);
alert(lati_array[0]);
</script>

Unable to print PHP variable using JS

I understand that this question has been asked a lot of of times, but the solutions posted there don't seem to work for me. I have a code as follows:
<script>
var JSvar = "<?php echo $phpVar ?>";
document.write(JSvar);
</script>
<?php
$phpVar="jajha";
?>
I actually want to pass a PHP variable to a JS function, but I wanted to try if printing the variable works first. Please help me out.
instead of
<script>
var JSvar = "<?php echo $phpVar ?>";
document.write(JSvar);
</script>
<?php
$phpVar="jajha";
?>
try
<?php
$phpVar="jajha";
?>
<script>
var JSvar = "<?php echo $phpVar ?>";
document.write(JSvar);
</script>

Reading session var in Javascript

How can I read the session data set in PHP from Javascript. I tried the following code, but it doesn't work.
<?php
session_start();
$_SESSION['test'] ="orange";
?>
<script>
var username = '<%= Session["test"] %>';
alert(username );
</script>
Thanks for your help.
Just echo it out:
<?php
session_start();
$_SESSION['test'] ="orange";
?>
<script>
var username = '<?php echo $_SESSION["test"]; ?>';
alert(username);
</script>
BTW, you used ASP.NET inline expression tags. You can use <?= ?> for PHP if you have short tags enabled.
use:
<script>
<?php echo "var username = '".$_SESSION["test"]."';";
alert (username);
</script>
Following are your mistakes
You have written Session["test"] instead of $_SESSION['test']
you are using % symbol instead of ?
Try this:
<?php
session_start();
$_SESSION['test'] ="orange";
?>
<script>
var username = '<?= $_SESSION["test"] ?>';
alert(username );
</script>

How to pass var to include file and then use this var in include file using php , javascript?

How to pass var to include file (test.php) and then use this var in include file (test.php) ?
First i want to declare var in main.php and then i want to pass this var to test.php and use this var in test.php (EG: echo , if..else)
I try with php and javascript but not work both , Could you please give me some advice ?
using PHP
main.php
<?php
include('test.php');
$number = '555';
test($number);
?>
test.php
<?php
function test($numeric)
{
return $sample = $numeric;
}
echo test($number);
?>
Using JS
main.php
<?php
include('test.php');
$number = '555';
?>
<script type="text/javascript">
doCallAjax();
</script>
test.php
<script type="text/javascript">
function doCallAjax() {
var number = "<?PHP echo $number; ?>";
alert(number);
}
</script>
if you want to share your data on multiple php files, consider to use the global variable or session, so it will be something like this:
SESSION:
main.php:
$_SESSION['number'] = '555';
test.php:
echo $_SESSION['number'];
GLOBAL VARIABLES:
main.php
global $number;
$number = '555';
test.php
global $number;
echo $number;

using php _GET within javascript function

I have some javascript embedded into an html file that I am running in a browswer.
document.getElementById('home-search-text-inp').value = <?php echo htmlspecialchars($_GET['search_for']); ?>;
Why does this not fill the textbox?
Note that:
document.getElementById('home-search-text-inp').value = "hi";
puts "hi" into the textbox and:
<?php echo htmlspecialchars($_GET['search_for']); ?>
writes text just fine.
Thanks in advance
You're missing quotes around your string value:
document.getElementById('home-search-text-inp').value = <?php echo htmlspecialchars($_GET['search_for']); ?>;
^^^^ ^^^^
HERE HERE
should be:
document.getElementById('home-search-text-inp').value = "<?php echo htmlspecialchars($_GET['search_for']); ?>";

Categories