This question already has answers here:
Convert php array to Javascript
(21 answers)
PHP array() to javascript array() [duplicate]
(9 answers)
Closed 7 years ago.
I get array from php,and then use to change js array and display but its show all elements as an array.##
$name = array('A','B','C','D');
<script>
<?php echo "var name='".json_encode($name)."';"; ?>
for (var i in name){
alert(name[i]);
}
</script>
<script type='text/javascript'>
<?php
$name = array('A','B','C','D');
$js_array = json_encode($name );
echo "var javascript_array = ". $js_array . ";\n";
?>
</script>
Reffer this Answer
Note
json_encode() is only available in PHP 5.2 and up
No need to wrap the value in '', if you wrap it the value will be considered as a string not an array in javascript
<?php echo "var name=".json_encode($name).";"; ?>
Related
This question already has answers here:
How do I pass variables and data from PHP to JavaScript?
(19 answers)
Closed 7 years ago.
I want to use a php variable in .js file. Is this possible that we can call a php variable in .js file.
what I have done for this problem
I have .php file where i created var
$domainname = "wwww.google.com ";
In second file .php
<?php header("Content-type: application/javascript"); ?>
<script>
var DomainName = <?php echo $GLOBALS['SettingDomainName'];?>
</script>
and finally I want to call this var in js
<script src="public_https/test.js"></script>
alert(DomainName);
I got this message :
SyntaxError: syntax error
<script src="public_https/test.js"></script>
Use json_encode function which returns JSON representation of a variable which can be used inside JavaScript as-is:
<?php
$domainname = "www.google.com";
?>
<script>
var DomainName = <?php echo json_encode($domainname); ?>;
</script>
Note: no quotes. This function works for all types of variable: string, integer, float, boolean, null and etc.
<script>
var DomainName = "<?php echo $GLOBALS['SettingDomainName'];?>";
</script>
What about we hide your variable in a hidden box using php
$domainname = "wwww.google.com ";
<input type="hidden" value=" <?php echo "$domainname"; ?>" id="mydomain">
Then we fetch it using js or jquery
<script>
var domain=$('#mydomain').val();
</script>
<input type="hidden" id="domain_name" value="<?php echo $GLOBALS['SettingDomainName']; ?>">
JsFile:
var DomainName= document.getElementById('domain_name');
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
script
<script type="text/javascript" >
var myvar = <?= json_encode($str); ?>;
alert(myvar);
</script>
php
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<?
$str = "ABCD";
echo "ENCODING: " . mb_detect_encoding($str) . "\n";
?>
im not good at english sorry.
$str = "ABCD"
i want to send string from php to javascript
var myvar = <?= json_encode($str); ?>;
i use json_encode but when i alert no massage display in alert-box(box happen but no massage there maybe null value)
alert(myvar);
i dont know what happen please help me
PS.alert for test string i want to use that string in javascript code
There are some issues with your code try to fix those as below :
Put the second <script> tag after PHP blocks.
Replace <? ?> blocks with <?php ?> or <?= ?>
Like so :
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<?=
$str = "ABCD";
echo "ENCODING: " . mb_detect_encoding($str) . "\n";
?>
<script type="text/javascript" >
var myvar = <?= json_encode($str); ?>;
alert(myvar);
</script>
Here are 2 flaws in your code
syntex to use php is <?php $str = "ABCD"; ?> not <?$str = "ABCD"?>
echo "ENCODING: " . mb_detect_encoding($str) . "\n"; is giving error , so it will not run
PHP code :-
<?php
$str = "ABCD";
$message = json_encode($str);
?>
Javacript code:-
<script type="text/javascript" >
var json = <?= json_decode($message); ?>;
var obj = JSON.parse(json);
alert(obj);
</script>
This question already has answers here:
Use a PHP variable in JQuery
(4 answers)
Closed 8 years ago.
can you help me
i have problem insert variable php into jquery, this my script:
<script type="text/javascript">
$("#add").click(function() {
var error = <?php echo $reportErr; ?>;
$("#table").append("<tr><td> <input type='text' name='report[]' /> <a href='javascript:void(0);' class='rem'>-</a>"+error+"</td></tr>");
});
$("#table").on('click','.rem',function() {
$(this).parent().parent().remove();
});
<?php
$report = "error";
$reportErr = $report;
?>
At first glance, you have a syntax error. Your string needs to be in quotes.
Try var error = "<?php echo $reportErr; ?>";
This question already has answers here:
Pass a PHP string to a JavaScript variable (and escape newlines) [duplicate]
(14 answers)
Closed 8 years ago.
I'm trying to pass a php var that is a string using Javascript, but in the final result the string gets cmmented in html. Here's my code:
PHP:
$txtVar = "My text";
JavaScript:
var txt = '<?php echo $txtVar; ?>';
document.getElementById('MyDiv').innerHTML = txt;
HTML(result):
<div id="MyDiv"><!--?php echo $txtVar ; ?--></div>
I just want the string value to be printed in my html, withou the comments ()
First print the PHP variable value in another HTML entity like hidden input HTml tag and after that pick the hidden value using JavaScript and assign into your desire tag.
In Your page.
<input type="hidden" value="<?php echo $txtVar; ?>" id="phptext" name="phptext" />
JavaScript code:
document.getElementById('MyDiv').innerHTML = document.getElementById('phptext').value;
This is works.
Here is your answer.
<?php
$txtVar = "My text";
?>
<div id="MyDiv"></div>
<script>
var txt = "<?php echo $txtVar; ?>";
var element = document.getElementById('MyDiv');
element.innerHTML = txt;
</script>
If your javascript is an external file, you can just rename it with .php extension, and then retrieve it with
<script language='javascript' type='text/javascript' src='yourscript.php'></script>
This question already has answers here:
How to access PHP variables in JavaScript or jQuery rather than <?php echo $variable ?> [duplicate]
(6 answers)
Access PHP variable in JavaScript [duplicate]
(3 answers)
Closed 9 years ago.
Hello I am begginner in php.
I create a ecommerce site .
first I fetch all information from database.
all is going correct but I have problem in quantity input
when I insert quantity value then the total calculate in total .
I use js but I don't know to fetch the price value from database and store in javascript variable.
the code is :
<?php
echo "ISBN:". $row['isbn']."<br/>";
echo "Publisher:". $row['publisher']."<br/>";
echo "Year:". $row['year']."<br/>";
echo "Price:". $row['price']."<br/>";
?>
Qty : <input type="text" name="qty1" id="qty"/><br>
Total : <input type="text" name="total" id="total"/>
Sum
<script>
window.sumInputs = function() {
var inputs = document.getElementsByTagName('input'),
result = document.getElementById('total'),
sum = 0;
var q = $row['price']; //how to resolve this line
for(var i=0; i<inputs.length; i++) {
var ip = inputs[i];
if (ip.name && ip.name.indexOf("total") < 0) {
sum = parseInt(ip.value)*q || 0;
}
}
result.value = sum;
}
</script>
you can combine javascript and php functionality by simply telling the parser to "print into the javascript". when the html file is "viewed" by the client, the javascript is beeing executed.
var q = parseFloat(<?php echo $row['price']; ?>);
pay attention that $row['price'] contains a number with a . as decimal dot - like 10.33 and the line
; ?>;
fist ; is the end from the php variable declation and second ; - after ?> is to end the variable declation from javascript.
Note - this only works when the Javascript Code an the HTML content is on the same Page. When dealing with an external Javascript Source - write in your PHP something like this
echo '<script>var q = "' . $row['sum'] . '";</script>";
and then use q in your external js file