0

I have a parameter of PHP of $test1, I want to add some scenario in JS which if $test1 exists, it will show some coding in JavaScript. I have drafted some code as follow and it doesn't work. What may it cause?

        <script type="text/javascript"><!--
            myChart.setXMLData("
            "<categories>" +
                "<category label='1'/>" +
                "<category label='2'/>" +
            "</categories>" +
            if($test1['transactions_tmp']) {
            "<dataset>" +
                "<set value='147400'/>" +
                "<set value='189100'/>" +
            "</dataset>" +
            }
            "</chart>");
        // -->
        </script>
5
  • 2
    You still have the XML comment start block in there: <!-- on the first line Commented Mar 6, 2014 at 6:35
  • 1
    ^ point 1, point 2 -> PHP variables are not accessible in HTML or javascript unless you are using templating. means generating the above code in php i.e echo jsCodeWIthPHPVariable; Commented Mar 6, 2014 at 6:38
  • I didn't catch your answer, but I think you should do it with Ajax Commented Mar 6, 2014 at 6:51
  • @HGF Yes, I have to embed also the xml in JS Commented Mar 6, 2014 at 8:12
  • @Kermani As I am using an add-on, I have to embed xml under JS. While for my case, I still have to pass the parameter from PHP defined to control whether I need the xml code case by case. Commented Mar 6, 2014 at 8:14

3 Answers 3

2

Try this:

<script type="text/javascript">
            myChart.setXMLData(
            "<categories>" +
                "<category label='1'/>" +
                "<category label='2'/>" +
            "</categories>" +
            "<?php if($test1['transactions_tmp']) { ?>" +
            "<dataset>" +
                "<set value='147400'/>" +
                "<set value='189100'/>" +
            "</dataset>" +
            "<?php } ?>" +
            "</chart>");

</script>
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. Tried but fail.
1
        <script type="text/javascript"><!--
            myChart.setXMLData("
            "<categories>" +
                "<category label='1'/>" +
                "<category label='2'/>" +
            "</categories>" +
            if($test1['transactions_tmp']) {
            "<dataset>" +
                "<set value='147400'/>" +
                "<set value='189100'/>" +
            "</dataset>" +
            }
            "</chart>");
        // -->
        </script>

this code is javasscript, if you wanna insert code php between javascript and php, you shoult use tag

<?php samp_code_php; ?>

replace if($test1['transactions_tmp']){ to <?php if($test1['transactions_tmp']){ ?> and } to < ?php } ?>

Comments

0

I'd give it a shot:

<script type="text/javascript">
    myChart.setXMLData(
    "<categories>" +
        "<category label='1'/>" +
        "<category label='2'/>" +
    "</categories><?php if($test1['transactions_tmp']) { echo '<dataset><set value=\'147400\'/><set value=\'189100\'/></dataset>';}?></chart>");
</script>

Comments

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.