Can I please have some help in using a PHP variable in some Javascript code.
I am wanting to pass a value to a Javascript function that is stored in a PHP variable.
Here is my code:
$UpdateText="updateReTotal(Tills,'pos_cash','{$till->till_id}');updateVariance('{$till->till_id}')";
echo '<script type="text/javascript">'
, 'testFunctionForUpdateTotal(.$UpdateText.);'
, '</script>';
Thanks in advance.
asked Feb 4, 2013 at 22:48
user2023359
2892 gold badges10 silver badges18 bronze badges
2 Answers 2
$UpdateText="updateReTotal(Tills,'pos_cash','{$till->till_id}');updateVariance('{$till->till_id}')";
echo '<script type="text/javascript">'
. 'testFunctionForUpdateTotal('.$UpdateText.');'
. '</script>';
Your code would have worked if you were using double quotes (except for the two dots beside your variable, plus you need to escape the other double quotes), like this:
echo "<script type=\"text/javascript\">
testFunctionForUpdateTotal($UpdateText)
</script>";
But this does not work with single quotes.
answered Feb 4, 2013 at 22:49
Green Black
5,0571 gold badge19 silver badges29 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
<script language='javascript'>
function show(j)
{
alert(j);
}
</script>
<?php
$v = "a";
echo "<div onclick=\"show('".$v."',);return false;\">CLICK</div>";
?>
answered Feb 4, 2013 at 22:55
Ali Mohammadi
1,3332 gold badges15 silver badges29 bronze badges
Comments
Explore related questions
See similar questions with these tags.
default