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

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>

Related

Echoing a mulitple line message in alert function

I need to echo multiple line error in JS alert and I am using below code
Working code
$str = "This is a error1\\n";
$alert = $str."This is error2";
if(!empty($alert)){
?>
<script type="text/javascript">
alert('<?php echo $alert;?>');
</script>
<?php
}
but I need to work on an old written code so this has addslashes function before echo alert like below:
$str = "This is a error1\\n";
$alert = $str."This is error2";
$alert = addslashes($alert);
if(!empty($alert)){
?>
<script type="text/javascript">
alert('<?php echo $alert;?>');
</script>
<?php
}
Output : This is a error1\nThis is error2
I need this to print in two lines I have also tried with only \n but it is not working then.
I'd make javascript do the hard work.
<script type="text/javascript">
var alertArr = ["<?php echo $string1; ?>", "<?php echo $string2; ?>"];
alert(alertArr.join("\n"));
</script>
https://jsfiddle.net/0uwmmm3n/
(Magnificent random url by the way)
EDIT: less messy version, maybe more legitimate
<?php $alertArr = [$string1, $string2]; ?>
<script type="text/javascript">
var alertArr = <?php echo json_encode($alertArr); ?>;
alert(alertArr.join("\n"));
</script>
RE-EDIT: nah, too much work for a job so simple, two conversions is way too much
$str = "This is a error1\\n";
$alert = $str."This is error2";
$alert = addslashes($alert);
if(!empty($alert)){
?>
<script type="text/javascript">
alert('<?php echo $alert;?>');
</script>
<?php
}
I have used this code and it working fine according to your need
if still issue continues then clear your cache

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>

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 echo a PHP variable in JavaScript? [duplicate]

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>

Categories