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.

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.

1
<script language='javascript'>
function show(j)
{
alert(j);
}
</script>

<?php
$v = "a";
echo "<div onclick=\"show('".$v."',);return false;\">CLICK</div>";
?>

Your Answer

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.