0

The code attached below, works, which asks for input on two items, then on submit, puts the values into a csv file on the php server.

I'd like to LOAD the LAST previous results, now the LAST line in the csv file, back INTO the form as DEFAULTS for the next round (So the 2 fields are populated with the values, and user can either press SUBMIT again, or make a change, and press SUBMIT to submit the newly changed values). If you know some simple coding to ADD to the code, to do the job, i'd love to see it... THANKS

BELOW is the file "FormTest-002.php" which calls itself when submitted...

<?php
if($_POST['formSubmit'] == "Submit")
{
    $errorMessage = "";

    if(empty($_POST['formGum']))
    {
        $errorMessage .= "<li>You didn't enter your favourite gum</li>";
    }
    if(empty($_POST['formName']))
    {
        $errorMessage .= "<li>You didn't enter your name</li>";
    }

    $varGum = $_POST['formGum'];
    $varName = $_POST['formName'];

    if(empty($errorMessage)) 
    {
        $fs = fopen("gumdata.csv","a");
        fwrite($fs,$varName . ", " . $varGum . "\n");
        fclose($fs);        
        header("Location: SubmitSuccessful.html");
        exit;
    }
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html>
<head>
    <title>MyGUM Form</title>
</head>

<body>
        <b>v1.01/18jan25/fj</b>

    <?php
        if(!empty($errorMessage)) 
        {
            echo("<p>There was an error with your form:</p>\n");
            echo("<ul>" . $errorMessage . "</ul>\n");
        } 
    ?>
    <form action="FormTest-002.php" method="post">
        <p>
            What is your favorite Gum?<br>
            <input type="text" name="formGum" maxlength="35" value="<?=$varGum;?>" />
        </p>
        <p>
            What is your name?<br>
            <input type="text" name="formName" maxlength="35" value="<?=$varName;?>" />
        </p>                
        <input type="submit" name="formSubmit" value="Submit" />
    </form>
</body>
</html>
Fred
  • 129
  • 2
  • 11

2 Answers2

0

For write and read csv file, PHP has native function. use fgetcsv() and fputcsv()

Mochamad Arifin
  • 418
  • 5
  • 9
0

OK, I hope someone will find this very useful! It works and is a good starting point for CSV writing and reading, and using the last values entered as STARTING DEFAULTS for the next entry.

I still want to change the functions to use actual CSV functions fgetcsv() and fputcsv(), but it works!

<?php
###
$ver="1.07e/17jan27";
###
#To use:
#  -By Fred Johnston and put onto StackExchange.com 18jan27
#  -If you find this helpful, please UPVOTE me on stackexchange.com... God Bless
#  -Put this FormTest-005.php into /var/www/html/whatever
#  -Create a blank text file called gumdata.csv in same folder
#  -Create an index.php in same folder with contents below
#  -Set all wide open permissions FOR TEST... "sudo chmod 777 *"
#  -Browse to the folder eg http://127.0.0.1/whatever
#Contents of index.php follow... (remove # (COMMENT) at start of each line)
#<HTML>
#<HEAD>
#<TITLE>Gummy Page</TITLE>
#</HEAD>
#<body>
#<A HREF="./FormTest-005.php">FromTest-005.php (GummyForm TEST)</A><BR>
#</BODY>
#</HTML>
#
###
### HERE WE GO... First pass skips this section, as form NOT submitted yet
### ...after form submitted, THIS section then runs when called 2nd time
###
if($_POST['formSubmit'] == "Submit")
{
    $errorMessage = "";

    if(empty($_POST['formGum']))
    {
        $errorMessage .= "<li>You didn't enter your favourite gum</li>";
    }
    if(empty($_POST['formName']))
    {
        $errorMessage .= "<li>You didn't enter your name</li>";
    }

    $varGum = $_POST['formGum'];
    $varName = $_POST['formName'];

    if(empty($errorMessage)) 
    {
        $fs = fopen("gumdata.csv","a");
        fwrite($fs,$varName . ", " . $varGum . "\n");
        fclose($fs);        
        header("Location: SubmitSuccessful.html");
        exit;
    }
}
?>

<?php
###
### This section runs, and reads in and displays CURRENT csv file
### ...and will list the contents of it as it gets more rows
### ...it also sets variables so they can populate as DEFAULTS!!
###
$row = 1;
  echo "==========================================="."<br>";
  echo $ver . "<br>";
  echo "CONTENTS OF gumdata.csv...<br>";
  echo "==========================================="."<br>";
if (($handle = fopen("gumdata.csv", "r")) !== FALSE) {
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
        $num = count($data);
        if ($row <> 1){
          echo "======= ======= =======" . "<br>";
        }
        echo "$num fields in line $row:"."<br>";
        $row++;
        for ($c=0; $c < $num; $c=$c+2) {
            echo "&nbsp;&nbsp;&nbsp;&nbsp;Name:" . $data[$c] . "<br>";
            echo "&nbsp;&nbsp;&nbsp;&nbsp;Gum:" . $data[$c+1] . "<br>\n";
            $varName = $data[$c];
            $varGum = $data[$c+1];
        }
    }
    echo "==========================================="."<br>";
    echo "==========================================="."<br>";
    fclose($handle);
}
?>

<?php
### 
### The next section is the display of the form, populated with the LAST previously entered
### values, so can be used as base for the next entry added.  Once submitted, THIS
### file FormTest-005.php is RE-RAN and the top section then does a STUPID CHECK and if
### all ok, then writes the two entered fields into the gumdata.csv file
###
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html>
<head>
    <title>MyGUM Form</title>
</head>

<body>
    <?php
        if(!empty($errorMessage)) 
        {
            echo("<p>There was an error with your form:</p>\n");
            echo("<ul>" . $errorMessage . "</ul>\n");
        } 
    ?>

        <?php
          echo $ver; #show version
        ?>


    <form action="FormTest-005.php" method="post">
        <p>
            What is your favorite Gum?<br>
            <input type="text" name="formGum" maxlength="35" value="<?=$varGum;?>" />
        </p>
        <p>
            What is your name?<br>
            <input type="text" name="formName" maxlength="35" value="<?=$varName;?>" />
        </p>                
        <input type="submit" name="formSubmit" value="Submit" />
    </form>
</body>
</html>
halfer
  • 19,824
  • 17
  • 99
  • 186
Fred
  • 129
  • 2
  • 11