...

View Full Version : Reading and writing to text files



Grey Area
09-07-2009, 03:48 PM
I'm trying to make a form that saves values to text files and then using those values as data in a bar chart using google API.

This is the code i have so far.

<html>
<body>
<form action="Input.php" method="POST">
Team A:
<input type="text" name="TeamA" value="0" size="1">
<br>Team B:
<input type="text" name="TeamB" value="0" size="1">
<br>Team C:
<input type="text" name="TeamC" value="0" size="1">
<br>Team D:
<input type="text" name="TeamD" value="0" size="1">
<br>
<INPUT TYPE="Submit" NAME="btnUpdate" Value="Update">
</form>

</body>
</html>

Then input.php is this:

<html>
<body>

<?PHP

$TeamAFile = fopen("TeamA.txt", "r") or exit("Unable to open file!");
$TeamA = fgets($TeamAFile);
fclose($TeamAFile);

$TeamBFile = fopen("TeamB.txt", "r") or exit("Unable to open file!");
$TeamB = fgets($TeamBFile);
fclose($TeamBFile);

$TeamCFile = fopen("TeamC.txt", "r") or exit("Unable to open file!");
$TeamC = fgets($TeamCFile);
fclose($TeamCFile);

$TeamDFile = fopen("TeamD.txt", "r") or exit("Unable to open file!");
$TeamD = fgets($TeamDFile);
fclose($TeamDFile);

$NewTeamA = $TeamA + $_POST['TeamA'];
$NewTeamB = $TeamB + $_POST['TeamB'];
$NewTeamC = $TeamC + $_POST['TeamC'];
$NewTeamD = $TeamD + $_POST['TeamD'];

$TeamAFile = fopen(TeamA.txt, 'w') or die("can't open file");
fwrite($TeamAFile, $NewTeamA);
fclose($TeamAFile);

$TeamBFile = fopen(TeamB.txt, 'w') or die("can't open file");
fwrite($TeamBFile, $NewTeamB);
fclose($TeamBFile);

$TeamCFile = fopen(TeamC.txt, 'w') or die("can't open file");
fwrite($TeamCFile, $NewTeamC);
fclose($TeamCFile);

$TeamDFile = fopen(TeamD.txt, 'w') or die("can't open file");
fwrite($TeamDFile, $NewTeamD);
fclose($TeamDFile);

echo "Scores updated."
?>

</body>
</html>

And then the barchart is this:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<title>
Google Visualization API Sample
</title>

<?PHP

$TeamAFile = fopen("TeamA.txt", "r") or exit("Unable to open file!");
$TeamA = fgets($TeamAFile);
fclose($TeamAFile);

$TeamBFile = fopen("TeamB.txt", "r") or exit("Unable to open file!");
$TeamB = fgets($TeamBFile);
fclose($TeamBFile);

$TeamCFile = fopen("TeamC.txt", "r") or exit("Unable to open file!");
$TeamC = fgets($TeamCFile);
fclose($TeamACile);

$TeamDFile = fopen("TeamD.txt", "r") or exit("Unable to open file!");
$TeamD = fgets($TeamDFile);
fclose($TeamDFile);

?>

<script type="text/javascript" src="http://www.google.com/jsapi"></script>
<script type="text/javascript">
google.load('visualization', '1', {packages: ['barchart']});
var data;
var vis;

valA = <?= $TeamA ?>;
valB = <?= $TeamB ?>;
valC = <?= $TeamC ?>;
valD = <?= $TeamD ?>;
UpdateValues (valA, valB, valC, valD);

function UpdateValues (valA, valB, valC, valD) {
data.setValue(0, 1, valA);
data.setValue(1, 1, valB);
data.setValue(2, 1, valC);
data.setValue(3, 1, valD);
vis.draw(data, {title: 'Scores', legend: 'none'});
}

function drawVisualization() {
data = new google.visualization.DataTable();
data.addColumn('string', 'Year');
data.addColumn('number', 'Score');
data.addRows(4);
data.setValue(0, 0, 'Team A');
data.setValue(1, 0, 'Team B');
data.setValue(2, 0, 'Team C');
data.setValue(3, 0, 'Team D);
vis = new google.visualization.BarChart(document.getElementById('visualization'));
UpdateValues(3, 4, 6, 7);
}


google.setOnLoadCallback(drawVisualization);
</script>

</head>
<body style="font-family: Arial;border: 0 none;">
<div id="visualization" style="width: 300px; height: 300px;"></div>
</body>
</html>
However this doesn't seem to update the text files or read from them and I don't seem to be getting any errors using firebug.

mlseim
09-07-2009, 04:02 PM
I'm not sure if you need quotes here or not:

fopen("TeamA.txt", "w")

And the file permissions (CHMOD) need to be 0777 or 777.

Grey Area
09-08-2009, 03:14 PM
I've made it into only 2 files now.

The form remains the same but input.php is now:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<title>
Google Visualization API Sample
</title>

<?PHP

$TeamAFile = fopen("TeamA.txt", 'r') or exit("Unable to open file!");
$TeamA = fgets($TeamAFile);
fclose($TeamAFile);

$TeamBFile = fopen("TeamB.txt", 'r') or exit("Unable to open file!");
$TeamB = fgets($TeamBFile);
fclose($TeamBFile);

$TeamCFile = fopen("TeamC.txt", 'r') or exit("Unable to open file!");
$TeamC = fgets($TeamCFile);
fclose($TeamCFile);

$TeamDFile = fopen("TeamD.txt", 'r') or exit("Unable to open file!");
$TeamD = fgets($TeamDFile);
fclose($TeamDFile);

$NewTeamA = $TeamA + $_POST['TeamA'];
$NewTeamB = $TeamB + $_POST['TeamB'];
$NewTeamC = $TeamC + $_POST['TeamC'];
$NewTeamD = $TeamD + $_POST['TeamD'];

$TeamAFile = fopen(TeamA.txt, 'w') or die("can't open file");
fwrite($TeamAFile, $NewTeamA);
fclose($TeamAFile);

$TeamBFile = fopen(TeamB.txt, 'w') or die("can't open file");
fwrite($TeamBFile, $NewTeamB);
fclose($TeamBFile);

$TeamCFile = fopen(TeamC.txt, 'w') or die("can't open file");
fwrite($TeamCFile, $NewTeamC);
fclose($TeamCFile);

$TeamDFile = fopen(TeamD.txt, 'w') or die("can't open file");
fwrite($TeamDFile, $NewTeamD);
fclose($TeamDFile);

?>

<?PHP

$TeamAFile = fopen("TeamA.txt", 'r') or exit("Unable to open file!");
$TeamA = fgets($TeamAFile);
fclose($TeamAFile);

$TeamBFile = fopen("TeamB.txt", 'r') or exit("Unable to open file!");
$TeamB = fgets($TeamBFile);
fclose($TeamBFile);

$TeamCFile = fopen("TeamC.txt", 'r') or exit("Unable to open file!");
$TeamC = fgets($TeamCFile);
fclose($TeamCFile);

$TeamDFile = fopen("TeamD.txt", 'r') or exit("Unable to open file!");
$TeamD = fgets($TeamDFile);
fclose($TeamDFile);

?>

<script type="text/javascript" src="http://www.google.com/jsapi"></script>
<script type="text/javascript">
google.load('visualization', '1', {packages: ['barchart']});
var data;
var vis;

valA = "<?= $TeamA ?>";
valB = "<?= $TeamB ?>";
valC = "<?= $TeamC ?>";
valD = "<?= $TeamD ?>";

data.setValue(0, 1, valA);
data.setValue(1, 1, valB);
data.setValue(2, 1, valC);
data.setValue(3, 1, valD);
vis.draw(data, {title: 'Scores', legend: 'none'});

function drawVisualization() {
data = new google.visualization.DataTable();
data.addColumn('string', 'Year');
data.addColumn('number', 'Score');
data.addRows(4);
data.setValue(0, 0, 'Team A');
data.setValue(1, 0, 'Team B');
data.setValue(2, 0, 'Team C');
data.setValue(3, 0, 'Team D');
vis = new google.visualization.BarChart(document.getElementById('visualization'));
UpdateValues(3, 4, 6, 7);
}


google.setOnLoadCallback(drawVisualization);
</script>

</head>
<body style="font-family: Arial;border: 0 none;">
<div id="visualization" style="width: 300px; height: 300px;"></div>

</body>
</html>

When I run it I get an error saying "data is undefined".
Can anyone help?

Grey Area
09-09-2009, 12:40 PM
Anyone?

Grey Area
09-15-2009, 05:59 PM
Bump.

Fumigator
09-15-2009, 06:26 PM
Can you verify the stuff in your files is correct?

Grey Area
09-16-2009, 03:13 PM
Yes it is.

Grey Area
09-23-2009, 02:01 PM
Bump again. Can anyone give me some help?

mlseim
09-23-2009, 04:41 PM
What line does it say "data is undefined" in the error message?

Grey Area
09-24-2009, 06:09 PM
It's saying line 24 of the input.php file but I'm not sure what line that actually corresponds to.

mlseim
09-24-2009, 06:26 PM
I tried it on my website and get no errors.

I do see a problem here though ...

$TeamAFile = fopen(TeamA.txt, 'w') or die("can't open file");

Should be (with quotes) ... on all 4 of your write lines ...

$TeamAFile = fopen("TeamA.txt", 'w') or die("can't open file");

Grey Area
09-24-2009, 06:32 PM
Thanks. I've fixed that now but still getting the same error.

Does it work on your website then?

mlseim
09-24-2009, 08:30 PM
I don't get any output on my screen, because the Javascripting part doesn't work,
but I also don't get any PHP scripting errors.

Remove all Javascripting and see if the error goes away ...
maybe it's not a PHP error, but a JS error?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum