Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 13 of 13
  1. #1
    New Coder
    Join Date
    Aug 2009
    Posts
    20
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Reading and writing to text files

    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.
    Code:
    <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:
    PHP Code:
    <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:
    PHP Code:
    <!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.

  • #2
    Master Coder mlseim's Avatar
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,397
    Thanks
    8
    Thanked 1,078 Times in 1,069 Posts
    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.

  • #3
    New Coder
    Join Date
    Aug 2009
    Posts
    20
    Thanks
    2
    Thanked 0 Times in 0 Posts
    I've made it into only 2 files now.

    The form remains the same but input.php is now:
    PHP Code:
    <!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?

  • #4
    New Coder
    Join Date
    Aug 2009
    Posts
    20
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Anyone?

  • #5
    New Coder
    Join Date
    Aug 2009
    Posts
    20
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Bump.

  • #6
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    Can you verify the stuff in your files is correct?

  • #7
    New Coder
    Join Date
    Aug 2009
    Posts
    20
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Yes it is.

  • #8
    New Coder
    Join Date
    Aug 2009
    Posts
    20
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Bump again. Can anyone give me some help?

  • #9
    Master Coder mlseim's Avatar
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,397
    Thanks
    8
    Thanked 1,078 Times in 1,069 Posts
    What line does it say "data is undefined" in the error message?

  • #10
    New Coder
    Join Date
    Aug 2009
    Posts
    20
    Thanks
    2
    Thanked 0 Times in 0 Posts
    It's saying line 24 of the input.php file but I'm not sure what line that actually corresponds to.

  • #11
    Master Coder mlseim's Avatar
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,397
    Thanks
    8
    Thanked 1,078 Times in 1,069 Posts
    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");

  • #12
    New Coder
    Join Date
    Aug 2009
    Posts
    20
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Thanks. I've fixed that now but still getting the same error.

    Does it work on your website then?

  • #13
    Master Coder mlseim's Avatar
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,397
    Thanks
    8
    Thanked 1,078 Times in 1,069 Posts
    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?


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •