0

Here am getting data from mysql..

if (!empty($result1)) {
    while ($row1 = mysqli_fetch_array($result1)) {
        $caseno = $row1['cases'];
        echo "<b>" . $caseno . "<br>";
    }
}

and i want pass the data which is there in $caseno to my below JavaScript..

<script type="text/javascript">
    var gaugevalue = document.getElementById("$caseno");
    var myConfig2 = {
        "type": "gauge",
        "scale-r": {
            "aperture": 200, //Scale Range
            "values": "0:50:10" //and minimum, maximum, and step scale values.
        },
        "series": [{"values": [gaugevalue]}]
            //"series":[{"values":[40]}]

    };

    zingchart.render({
        id: 'myChart',
        data: myConfig2,
        height: "90%",
        width: "90%"
    });

</script>
3
  • Thanks Victor for your replay, still it is not working.. Commented Dec 15, 2018 at 6:15
  • Sorry, I did not notice that you do not have an element with such an identifier. So, check my update. Commented Dec 15, 2018 at 6:24
  • The gaugevalue must be an array of integers, not a single $caseno. I updated my answer. Commented Dec 15, 2018 at 6:40

4 Answers 4

2

I analyzed your code more attentively and noticed that gaugevalue must be an array of integers, while you are trying to pass to it a DOM element. So your full code should look like this:

<?php
$gauge_values = [];
if (!empty($result1)) {
    while ($row1 = mysqli_fetch_array($result1)) {
        $gauge_values[] = $row1['cases'];
    }
}
?>

<script type="text/javascript">
    var myConfig2 = {
        "type": "gauge",
        "scale-r": {
            "aperture": 200, //Scale Range
            "values": "0:50:10" //and minimum, maximum, and step scale values.
        },
        "series": [{"values": <?php echo json_encode($gauge_values); ?>}]
    };

    zingchart.render({
        id: 'myChart',
        data: myConfig2,
        height: "90%",
        width: "90%"
    });
</script>
Sign up to request clarification or add additional context in comments.

Comments

1

Try this way instead:

<script type="text/javascript">
    var gaugevalue = "<?php echo $caseno ?>";
    var gaugevalue = "<?= echo $caseno ?>"; //for shorthand
</script>

Comments

0

1st Method:

In your php code, you can do:

$caseno = $row1['cases'];

In your javascript code, you can do:

var gaugevalue = "<?php echo $caseno ?>";

2nd Method:

In your php code, you can do:

echo '<input type="hidden" id="caseno" value="'.$caseno.'">';

In your javascript code, you can do:

var gaugevalue = document.getElementById("caseno");

It's simple and easy.

2 Comments

Thanks Inzamam for your help.. i have modified according to your code, still not working..
you are coding in the same file or different? Please specify this also?
-1

put you JavaScript in php:

<?php
echo "
<script type="text/javascript">
    var gaugevalue = document.getElementById("$caseno");
    var myConfig2 = {
    "type":"gauge",
    "scale-r":{
    "aperture":200, //Scale Range
    "values":"0:50:10" //and minimum, maximum, and step scale values.
    },
    "series":[{"values":[gaugevalue]}]
    //"series":[{"values":[40]}]

    };

    zingchart.render({ 
    id : 'myChart', 
    data : myConfig2, 
    height : "90%", 
    width: "90%"
    });

</script>
"
?>

You can pass your variable like this.

2 Comments

Thanks toykam, am getting Parse error: syntax error, unexpected T_STRING, expecting ',
Escape the strings for it to work by changing the double qoutes in the JavaScript code.

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.