Jump to content

error in JS


felito

Recommended Posts

hi

 

i have this code

 

  <script type="text/javascript">
            $(document).ready(function(){
            
                $.ajax({
                    dataType: 'json',
                    url: "chart.php",
                    
                    success: function(json){
                        var msg = [];
                        for (i = 0; i < 2; i++) {
                            $msg[i] = parseFloat(eval('json[' + i + '].valor(' + i + '+1)'));
                            i++;
                        }
                    }
                });
            });
        </script>

 

and i am trying to access

                chartTwo.setTheme(davidwalsh.charting.themes.SitePen).addPlot("default", {
                    type: "Pie",
                    font: "normal normal 11pt Tahoma",
                    fontColor: "black",
                    labelOffset: -30,
                    radius: 80
                }).addSeries("Series A", [{
                    y: $msg[0], //problem
                    text: "Red",
                    stroke: "black",
                    tooltip: $msg[0]//problem
                }, {
                    y: $msg[1],//problem
                    text: "Green",
                    stroke: "black",
                    tooltip: $msg[1]//problem
                }, {
                    y: $msg[2],//problem
                    text: "Blue",
                    stroke: "black",
                    tooltip: $msg[2]//problem
                }, ]);

 

and i get this error

 

$msg is not defined

$msg = ...son[' + i + '].valor(' + i + '+1)'));

 

$msg is not defined

y: $msg[0],

 

any idea?

 

if i try with

$msg1 = parseFloat(json[0].valor1);

 

and

 

   y: $msg1, //solved

 

thanks

Link to comment
https://forums.phpfreaks.com/topic/234156-error-in-js/
Share on other sites

chart.php content:

 

$query = mysql_query("SELECT valor FROM grafico") or die(mysql_error());


$i = 0;
while($row = mysql_fetch_assoc($query)) {
    $arr[] = array("valor{$i}" => $row[valor]);
    ++$i;
}
echo json_encode($arr);

 

as i said, in my old code, all works well, the problem at the moment is access to array

 

thanks

Link to comment
https://forums.phpfreaks.com/topic/234156-error-in-js/#findComment-1203512
Share on other sites

I'm only picking at random as you've given a very vague description of the problem..

 

                        var msg = [];
                        for (i = 0; i < 2; i++) {
                            $msg[i] = parseFloat(eval('json[' + i + '].valor(' + i + '+1)'));
                            i++;

 

You're defining the array as var msg, but then trying to add to it as $msg - are you confusing your PHP, JS and jQuery? Variable names in JS can start with a $, but in your case you're just calling it msg...

Link to comment
https://forums.phpfreaks.com/topic/234156-error-in-js/#findComment-1203519
Share on other sites

In chart.php you're assigning the values like:

 

$arr[] = array("valor{$i}" => $row[valor]);

 

So you would end up with keys like "valor0", "valor1", "valor2", etc. In the JavaScript, once the array has been converted to JSON and passed back, you're then trying to access it like:

 

json[' + i + '].valor(' + i + '+1)

 

Which for example would be "json[0].valor(0+1)", "json[1].valor(1+1)", etc. None of this adds up..

 

Try this as the JavaScript loop:

 

for (x in json) {
    var valor = eval('json[' + x + '].valor' + x);
    msg[] = parseFloat(valor);
}

Link to comment
https://forums.phpfreaks.com/topic/234156-error-in-js/#findComment-1203533
Share on other sites

this give a syntax error in lint

 

        <script type="text/javascript">
                        $(document).ready(function(){
                        
                            $.ajax({
                                dataType: 'json',
                                url: "chart.php",
                                
                                success: function(json){
   
                                    for (var i = 0; i < 200; ++i) {
                                           var valor = eval('json[i].valor' + i);
                                   
               msg[] = parseFloat(valor); //here

            }
                                }
                            });
                        });
                    
        </script>

 

http://www.javascriptlint.com/online_lint.php

Link to comment
https://forums.phpfreaks.com/topic/234156-error-in-js/#findComment-1203545
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.