felito Posted April 19, 2011 Share Posted April 19, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/234156-error-in-js/ Share on other sites More sharing options...
Adam Posted April 19, 2011 Share Posted April 19, 2011 Well.. Where is $msg defined? I can see where you're assigning it in the AJAX success method, but what about in the JSON string I'm assuming the chart.php file returns? Quote Link to comment https://forums.phpfreaks.com/topic/234156-error-in-js/#findComment-1203506 Share on other sites More sharing options...
felito Posted April 19, 2011 Author Share Posted April 19, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/234156-error-in-js/#findComment-1203512 Share on other sites More sharing options...
Adam Posted April 19, 2011 Share Posted April 19, 2011 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... Quote Link to comment https://forums.phpfreaks.com/topic/234156-error-in-js/#findComment-1203519 Share on other sites More sharing options...
felito Posted April 19, 2011 Author Share Posted April 19, 2011 i am confused yes http://www.felito.summerhost.info/doj.htm demo on the air Quote Link to comment https://forums.phpfreaks.com/topic/234156-error-in-js/#findComment-1203524 Share on other sites More sharing options...
Adam Posted April 19, 2011 Share Posted April 19, 2011 Change: $msg[i] = parseFloat(eval('json[' + i + '].valor(' + i + '+1)')); To: msg[i] = parseFloat(eval('json[' + i + '].valor(' + i + '+1)')); Quote Link to comment https://forums.phpfreaks.com/topic/234156-error-in-js/#findComment-1203526 Share on other sites More sharing options...
felito Posted April 19, 2011 Author Share Posted April 19, 2011 now json[0].valor is not a function msg[i] = p...son[' + i + '].valor(' + i + '+1)')); msg is not defined y: msg[0], Quote Link to comment https://forums.phpfreaks.com/topic/234156-error-in-js/#findComment-1203527 Share on other sites More sharing options...
Adam Posted April 19, 2011 Share Posted April 19, 2011 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); } Quote Link to comment https://forums.phpfreaks.com/topic/234156-error-in-js/#findComment-1203533 Share on other sites More sharing options...
felito Posted April 19, 2011 Author Share Posted April 19, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/234156-error-in-js/#findComment-1203545 Share on other sites More sharing options...
felito Posted April 19, 2011 Author Share Posted April 19, 2011 var msgs = []; for (var i = 0; i < 200; ++i) msgs.push(parseFloat(json['valor' + i])); solved Quote Link to comment https://forums.phpfreaks.com/topic/234156-error-in-js/#findComment-1203681 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.