-2

i m having problem in using php variable in javascript and having problem in using javascript variable in php.

$UpdateText="updateReTotal(Tills,'pos_cash','{$till->till_id}');updateVariance('{$till->till_id}')";

echo '<script type="text/javascript">'
    , 'testFunctionForUpdateTotal(.$UpdateText.);'
    , '</script>';
1
  • You should NEVER directly output from PHP into Javascript. One ' or other JS-metacharacter in the PHP data and you've killed your JS block with a syntax error. Always output via json_encode() so you're at least generating valid JS. Commented Dec 16, 2013 at 14:51

3 Answers 3

2

You just have a quoting issue. :

$UpdateText="updateReTotal(Tills,'pos_cash','{$till->till_id}');updateVariance('{$till->till_id}')";

echo '<script type="text/javascript">'
    , "testFunctionForUpdateTotal('".$UpdateText."');"
    , '</script>';

This is a good example of why you should avoid using echo statements to output HTML. PHP is designed to allow you to embed PHP inside of your HTML and you should take advantage of that:

    $UpdateText="updateReTotal(Tills,'pos_cash','{$till->till_id}');updateVariance('{$till->till_id}')";
?>
    <script type="text/javascript">
        testFunctionForUpdateTotal('<?= $UpdateText; ?>');
    </script>';
5
  • 1
    <?php echo $UpdateText; ?> is preferred to the short code <?= $UpdateText; ?> as the PHP short code is disabled by default in most production environments. Commented Dec 16, 2013 at 14:53
  • Are you sure about that? As of PHP 5.4 they are enabled permanently. Commented Dec 16, 2013 at 14:54
  • For portability the long style is preferred as short tags make a dependency on PHP 5.4+ or having the short tags setting enabled stackoverflow.com/a/200666/2594742 Commented Dec 16, 2013 at 15:00
  • If you plan to distibute your app, sure. But that's is not the common case. Commented Dec 16, 2013 at 15:01
  • True, but I'm just raising it because for maximum compatibility the long way is preferred. Personally I prefer the short tags however. Commented Dec 16, 2013 at 15:07
0

If you use concatenation you can put the php variable into the javascript code, and you are using concatenation in your echo already.

php can't see the javascript variable as javascript is on the browser and php is on the server

your best bet is just to pass back the javascript variable

0

JavaScript is Client-side. PHP is Server-side. They can, somehow, talk, but it ain't always so easy.

To use a PHP variable in Javascript, you have to ways, I'd say. The easy way is:

var phpVar = "<?php echo $myVar; ?>";

Or, by echoing:

echo "var phpVar = '{$myVar}';";

But, you'll soon realize this will be printed a little like a constant. This can only be done on the page's rendering, so it's a little limited. It really is, like, if $myVar contains 5, to write this directly:

var phpVar = "5";

The results are the same. Another way to use a PHP var in JavaScript is to AJAX-get it. For example, with jQuery:

$.get("/page_that_sends_my_var.php", { }, function(res) {
    var phpVar = res;
});

By having page_that_sends_my_var.php something like this:

<?php
echo $myVar;

But that will be asynchronous. So, you have two different methods for two different purposes. The first one, synchronously, only works when the page is rendering; the second one works whenever you want through an asynchronous call. I'd probably always use the second method; It's more elegant, and AJAX is something nice to do / have: it makes your application more dynamic.

As for using a Javascript variable in PHP, you can do it in 2 ways too. The first one is to use a Form and include the variable in the form's data during the form's submit; the other one (which I prefer) is to use AJAX calls to a PHP script, and send the variable. Like this:

$.post("page_that_uses_my_var.php", { "myVar" : myVar }, function(res) {
    // do something if you like
});

Then, in page_that_uses_my_var.php:

<?php
$jsVar = $_POST["myVar"];

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.