0

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
0

2 Answers 2

5
$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
Sign up to request clarification or add additional context in comments.

Comments

1
<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

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.