0

This is working, after all it's a syntax error, but I was wondering how could one do this so that the syntax is proper.

The code below is part of a script that is preparing variables to be sent on a Ajax call, so depending on the scope the corresponding variable should be passed on to Ajax.

Problem, from javascript perspective, it seems, is that only the php variable for each scope is defined.

I would think that it would not try at all to parse if the else if condition is not met, but apparently that is not the case. The php variable $scope is always defined.

Both firebug and Chrome developer tool are throwing the error:

                    var scope_var = <br />
<b>Notice</b>:  Undefined variable: brand in <b>/Applications/MAMP/htdocs/my_site/my_script.php</b> on line <b>1049</b><br />null;

Thanks

        var scope = <?php echo json_encode($scope) ;?>;
        if (scope == 'all') {var scope_var = 'all';}
        else if (scope == 'brands') {var scope_var = <?php echo json_encode($brand) ;?>;}
        else if (scope == 'cities') {var scope_var = <?php echo json_encode($city) ;?>;}
        else if (scope == 'models') {var scope_var = <?php echo json_encode($model) ;?>;}
        else if (scope == 'prange') {var scope_var = <?php echo json_encode($used_prange_low) . '|' . json_encode($used_prange_high) ;?>;}
2
  • use single quotes var scope_var =' <?php echo json_encode($brand) ;?>'; Commented Apr 28, 2016 at 22:21
  • are you sure that $brand is defined? try printing it before the if statements. Commented Apr 28, 2016 at 23:01

1 Answer 1

1

From the look of your code you're expecing the values you're outputting from PHP to JS to be received as strings. If this is the case you need to wrap all the PHP statements in quotes. Try this:

var scope = '<?php echo json_encode($scope); ?>';
if (scope == 'all') {
    var scope_var = 'all';
} else if (scope == 'brands') {
    var scope_var = '<?php echo json_encode($brand); ?>';
} else if (scope == 'cities') {
    var scope_var = '<?php echo json_encode($city); ?>';
} else if (scope == 'models') {
    var scope_var = '<?php echo json_encode($model); ?>';
} else if (scope == 'prange') {
    var scope_var = '<?php echo json_encode($used_prange_low) . ' | ' . json_encode($used_prange_high); ?>';
}

Also note that a switch statement may suit your needs better than multiple else if conditions.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, adding the quotes only changes the error, or rather notice. It now becomes "unterminated string literal". So that will not solve it. But I would be interested on the switch thing. That sounds more promising.
switch will not cut it either. Still the same error. It seems unable to handle if the variable is not defined to start with, even if condition not met
You can use JSON_HEX_QUOT option of json_encode. Here the example json_encode($scope, JSON_HEX_QUOT). It will manage the quotes and "unterminated string literal" problem will get solve. Hope it may help.

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.