Matt Hutch Matt Hutch - 2 months ago 16
Javascript Question

Reading data from directory PHP to Jquery chart

I'm trying to make my php set a variable from a text document in my site directory, then using my JQuery to set a variable using this directory. However this isn't working as its making the graph go blank. Also I'm trying to do the same thing with the textbox where it sets its value from the same file. Neither of these areas working. However I have commented it out but if I use a simple number in the php variable everything works. Now I know that there is in fact a value in the text file as if I echo after setting the variable from the file I get an output of what I wanted. So why isn't the chart isn't getting this output or the textbox.

FILE EXAMPLE

http://server1.sulmaxcp.com/value1.txt

CONSOLE ERROR

Uncaught SyntaxError: Invalid or unexpected token on line 13:

var pValue1 = '1

PHP

<?php
$myFile=fopen("CHARTS/value1.txt","r") or exit("Can't open file!");
$pValue1 = fgets($myFile);
fclose($myFile);

//$pValue1 = 5;
?>




window.onload = function() {
var pValue1 = '<?php echo $pValue1; ?>';
var bar = new RGraph.Bar({
id: 'cvs1',
data: [pValue1, 2, 3, 4, 5, 6, 7, 8, 9],
options: {
backgroundGridDashed: true,
labels: ['Mal', 'Barry', 'Gary', 'Neil', 'Kim', 'Pete', 'Lou', 'Fred', 'Jobe'],
title: 'A dashed background grid',
strokestyle: 'rgba(0,0,0,0)',
textAccessible: true
}
}).draw();
};

#btn_Charts {
width: 100px;
margin-top: 10px;
background: red;
border: 1px solid red;
border-radius: 5px;
text-align: center;
text-decoration: underline;
font-weight: bold;
color: white;
}

<script src="http://server1.sulmaxcp.com/RGraph.common.core.js"></script>
<script src="http://server1.sulmaxcp.com/RGraph.bar.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<canvas id="cvs1" width="600" height="250">[No canvas support]</canvas>
<form method="post" action="Charts.php" id="Chart1" autocomplete="off">
<input type="number" id="tb_1" name="tb_1" <?php echo 'value="'.$pValue1. '"';?>>
<div id="btn_Charts" onclick="javascript:this.parentNode.submit();">
<p>Update</p>
</div>
</form>




Answer

As your Javascript requires integers for the Graph generation then I suggest changing the following:

var pValue1 = '<?php echo $pValue1; ?>';

to

var pValue1 = <?php echo $pValue1; ?>;