How to echo a PHP variable in JavaScript? [duplicate] - javascript

This question already has answers here:
Pass a PHP string to a JavaScript variable (and escape newlines) [duplicate]
(14 answers)
Closed 9 years ago.
I need to echo a PHP variable into JavaScript. But I have no experience about JavaScript, I did some research but nothing worked for me.
The code
function hide() {
document.getElementById("myDiv").style.display="none";
<?php echo $myVar; ?>
}
And the $myVar looks like:
$myVar = "Hi there";

you can do like this in js :
var php_val = '<?php echo $myVar; ?>';
alert(php_val);

use this
$myVar = 'var myVar="Hi here"'
or
function hide() {
document.getElementById("myDiv").style.display="none";
var myVar='<?php echo $myVar; ?>'
}

There is 2 ways to write PHP variable in javascript:
First Way:
JavaScript written in the php page or inline:
EX:
<?php
$myVar = 'hello';
?>
....
<script>
function hide() {
document.getElementById("myDiv").style.display="none";
<?php echo $myVar; ?>
}
</script>
Second way:
Put your variables on the top of your page, like baseUrl for example, and use it in separated JavaScript file but you have to write the variables before load JavaScript files.
EX:
<head>
<script>
var baseUrl = <?php echo $thisPageUrl; ?>
</script>
</head>
....
<script src="myscriptfile.js"></script>

<?php $var="blah"; ?>
<script type="text/javascript">
alert('<?php echo $var ?>');
</script>

Related

display php array in javascript [duplicate]

This question already has answers here:
Pass a PHP string to a JavaScript variable (and escape newlines) [duplicate]
(14 answers)
Closed 5 years ago.
I like to display php array in javascript, but in specific element with id.
below is my code which shows what I want to do.
<?php function ogolne_informacje(){
global $mypod;?>
<strong><?php echo 'Pokój: '?></strong><?php echo $mypod->display('room')."<br />";?>
<strong><?php echo 'Email: '?></strong><?php echo $mypod->display('user_email')."<br />";?>
<strong><?php echo 'Telefon: '?></strong><?php echo $mypod->display('phone')."<br />"; }?>
<?php $i =0;
while ( $mypod->fetch() ) :{
ob_start();
ogolne_informacje();
$output[$i] = ob_get_clean();
$i++;
} endwhile;?>
<div id="test"></div>
<script>
var out = <?php echo $output; ?>;
$(document).ready(function () {
$('#test').html(out.toString());
});
</script>
How can I do that?
Thanks!
You can't loop like that, you need to loop the PHP array and push into javascript array:
<script type="text/javascript" language="javascript">
var pausecontent = new Array();
<?php while ( $mypod->fetch() ) :{
ob_start();
ogolne_informacje();
?>
pausecontent.push('<?php echo ob_get_clean(); ?>');
<?php } ?>
</script>

document.title funcion can't set it work with php

I have this line of code
<script type="text/javascript">
document.title = "<?php include('pot1.php');?>"
</script>
My pot1.php gives me an echo with one random number, but that code does not seem to work and I don't know why the echo doesn't appear in my title. Is anything wrong with the code?
pot1.php code:
<?php
#include_once('link1.php');
$cg = fetchinfo("value","info","name","current_game");
$cb = fetchinfo("cost","games","id",$cg);
$cb=round($cb,2);
echo '$'.$cb;
?>
This echo is working and is giving me values because I can see them in one div.
You should just do it like this:
<?php include('pot1.php');?>
Now we know that there is a variable set. Echo that into the title tag.
<script type="text/javascript">
document.title = "<? echo $cb ?>"
</script>
The way you're doing it now... I don't think you can include an entire PHP page and expect to behave like a simple string, even if that's all it returns.
Put the value you want inside a variable in the pot1.php file and use it on the title
<?php include('pot1.php');?>
<script type="text/javascript">
document.title = "<?php echo $yourVar;?>"
</script>

How to assign php variable value into javascript variable [duplicate]

This question already has answers here:
How do I pass variables and data from PHP to JavaScript?
(19 answers)
Closed 7 years ago.
I have below js code
<script type="text/javascript">
var jvalue = 'Hi, $name';
</script>
And this is my php code
<?php
$name= "Rama";
$abc = "<script>document.write(jvalue)</script>";
echo $abc;
?>
This is giving me output
Hi, $name
Instead of
Hi, Rama
echo it. But Remember to follow the sequence of definition -
<?php
$name= "Rama";
?>
<script type="text/javascript">
var jvalue = 'Hi, <?php echo $name; ?>';
</script>
<?php
$abc = "<script>document.write(jvalue)</script>";
echo $abc;
?>
Try This one
<script type="text/javascript">
var jvalue = "Hi, <?=$name;?>";
</script>
User double cot.
or best you can assing that php varible in input like this
<input type=hidden value="<?=$name; ?>" id='name'>
so you can access value of input using id anywhare on page.
your script code will be
<script src="//code.jquery.com/jquery.min.js"></script>
<script type="text/javascript">
$(function(){
var jvalue = 'Hi, '+$('#name').val()+'';
});
</script>
I hope it will work for u as easy solution for now and for future.
simple... try this
<?php
$variable = "JS";
?>
<script>
var field = <?php echo json_encode($variable); ?>;
</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>

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;

Categories