Jack_R
Jack_R

Reputation: 3

How can I save form data to a different filename for each submission?

I have a form that emails data and saves it into a file, but I want it to save to a different file each time.

Here is my code:

<?php
    // Contact Form

    // get posted data into local variables
    $EmailFrom = "gmail.com";
    $EmailTo = "[email protected]";
    $Subject = "$Website";
    $Website = Trim(stripslashes($_POST['Website'])); 
    $Title = Trim(stripslashes($_POST['Title'])); 
    $Keywords = Trim(stripslashes($_POST['Keywords'])); 

    // validation
    $validationOK=true;
    if (!$validationOK) {
      print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
      exit;
    }

    // prepare email body text
    $Body = "";
    $Body .= "Website: ";
    $Body .= $Website;
    $Body .= "\n";
    $Body .= "Title: ";
    $Body .= $Title;
    $Body .= "\n";
    $Body .= "Keywords: ";
    $Body .= $Keywords;
    $Body .= "\n";

    // send email 
    $success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");

    // redirect to success page 
    if ($success){
        $string = '"Website","Title","Keywords"' . PHP_EOL;
        $string .= "\"$Website\",\"$Title\",\"$Keywords\"" . PHP_EOL;
        file_put_contents('formdata.txt', $string); // write file
        print "<meta http-equiv=\"refresh\" content=\"0;URL=ok.html\">";
    }

    else{
      print "<meta http-equiv=\"refresh\" content=\"0;URL=error.html\">";
    }
?>

Upvotes: 0

Views: 232

Answers (1)

jrn.ak
jrn.ak

Reputation: 36619

Here's a quick solution that uses the current time:

file_put_contents('formdata_' . time() . '.txt', $string); // write file

Upvotes: 1

Related Questions