1.

Solve : How to save data from a PHP form?

Answer»

Hi 
     Everybody,

                          I want to use a php form to save the data inputted to it ,within the hosting server as simple text file. As most of the hosting services don't provide mailing PHP and even if so then the mail address must of the of the same domain for which the hosting server is being used. So i wondering is there any way to save the information  instead of sending via email. Kindly Please help me.                    

                                                    The Form Code  
      "http://www.w3.org/TR/html4/strict.dtd">


Your Informations




Your Name:

Your Email:

Your Message:
 







                                                          The PHP code for the form
$to = "[email protected]";
$subject = "Results from your Request Info form";
$headers = "From: Form Mailer";
$forward = 1;
$location = "http://XXXXXXXX.com";

$date = date ("l, F jS, Y");
$time = date ("h:i A");



$msg = "Below is the result of your feedback form. It was submitted on $date at $time.\n\n";

if ($_SERVER['REQUEST_METHOD'] == "POST") {
   foreach ($_POST as $key => $value) {
      $msg .= ucfirst ($key) ." : ". $value . "\n";
   }
}
else {
   foreach ($_GET as $key => $value) {
      $msg .= ucfirst ($key) ." : ". $value . "\n";
   }
}

mail($to, $subject, $msg, $headers);
if ($forward == 1) {
    header ("Location:$location");
}
else {
    echo "Thank you for submitting our form. We will get back to you as SOON as POSSIBLE.";
}

?>



    Above form and PHP script work fine for servers providing an email PHP script. But most of the free hosting services are not providing it anymore. So it will also work for me if i COULD make an arrangement to save the inputs of the form..

                    What Minimal Modification in the script can serve the PURPOSE                                                   

             
Thanks.....

                                       Try something like this...

HTML Form Code:

Code: [Select]<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Your Informations</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<form action="mailer.php?savedata=1" method="post">
Your Name: <input type="text" name="name"><br>
Your Email: <input type="text" name="email"><br>
Your Message:<br> <textarea name="message" rows="5" cols="30"></textarea><br>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>

PHP Code:
Code: [Select]<?php
$savedata = $_REQUEST['savedata'];
if ($savedata == 1){ 
$data = $_POST['name'];
$data = $_POST['email'];
$data = $_POST['message'];
$file = "YOURDATAFILE.txt"; 

$fp = fopen($file, "a") or die("Couldn't open $file for writing!");
fwrite($fp, $data) or die("Couldn't write values to file!"); 

fclose($fp); 
echo "Your Form has been Submitted!";

}
?>
Quote from: fffreak on December 29, 2008, 03:19:08 PM

PHP Code:
Code: [Select]<?php
$savedata = $_REQUEST['savedata'];
if ($savedata == 1){ 
$data = $_POST['name'];
$data .= $_POST['email'];
$data .= $_POST['message'];
$file = "YOURDATAFILE.txt"; 

$fp = fopen($file, "a") or die("Couldn't open $file for writing!");
fwrite($fp, $data) or die("Couldn't write values to file!"); 

fclose($fp); 
echo "Your Form has been Submitted!";

}
?>

You forgot one small thing there - a full STOP for the $data variable. If you don't have the full stop you will get a PHP error.Thanks for catching that, I was quite tired when typing that up, plus I was at work . Quote from: fffreak on December 30, 2008, 02:12:58 PM
Thanks for catching that, I was quite tired when typing that up, plus I was at work .

Alright.

We all make small mistakes like that. I know, it's a old thread, but I have a question here. This form glues the result to one string like,

Name1Email1The Message1Name2Email2The Message2Name3Email3The Message3Name4Email4The Message4

I normaly don't use PHP, now I need this code, but without that error. I read some PHP stuff but couldn't find a solution.

Thanks for your help.I got a very fast answer on Stackoverflow.
http://stackoverflow.com/questions/12908334/new-line-in-php-output-in-a-text-file

Quote
$data = $_POST['name'] . PHP_EOL;
$data .= $_POST['email'] . PHP_EOL;
$data .= $_POST['message'] . PHP_EOL;
Quote from: DarthSeverus on October 16, 2012, 08:45:06 PM
I got a very fast answer on Stackoverflow.
http://stackoverflow.com/questions/12908334/new-line-in-php-output-in-a-text-file

Either that or use this:
Code: [Select]$data = $_POST['name'] . "\n";
$data .= $_POST['email'] . "\n";
$data .= $_POST['message'];
...where \n means "new line".


Discussion

No Comment Found