简体   繁体   中英

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 = "mail@gmail.com";
    $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\">";
    }
?>

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM