jeudi 25 janvier 2018

Loading csv values from a file into web form as DEFAULTS

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 previous results, now in the csv file, back INTO the form as defaults for the next round.

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

    if(empty($_POST['formGum']))
    {
        $errorMessage .= "<li>You forgot to enter your favourite gum!</li>";
    }
    if(empty($_POST['formName']))
    {
        $errorMessage .= "<li>You forgot to 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: thankyou.html");
        exit;
    }
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html>
<head>
    <title>My Form</title>
</head>

<body>
    <?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="50" value="<?=$varGum;?>" />
        </p>
        <p>
            What is your name?<br>
            <input type="text" name="formName" maxlength="50" value="<?=$varName;?>" />
        </p>                
        <input type="submit" name="formSubmit" value="Submit" />
    </form>
</body>
</html>




Aucun commentaire:

Enregistrer un commentaire