Home > Blockchain >  html form will not write to file (php script)
html form will not write to file (php script)

Time:01-16

I have an HTML file that contains the following form:

<form action="" >
<div >
<input name="username" type="text"  placeholder="Name">
</div>
<div >
<input name="email" type="text"  placeholder="Email">
</div>
<div >
<textarea rows="5" name="message"  placeholder="Message"></textarea>
</div>
<div >
<input type="submit"  value="Save">
</div>
</form>

It seems fine to me. I also have a php script:

> <?php
extract($_REQUEST);

> $file=fopen("form.txt.", "a");
fwrite($file, "----");
fwrite($file," name :");
fwrite($file, $username ."\n");
fwrite($file," email :");
fwrite($file, $email ."\n");
frwite($file," message .:");
fwrite($file, $message ."\n");

fclose($file);
> ?>

Both are in the same file, the form in between html tags, the php script after the /html tag.

If will not execute. No matter what I do, form.txt remains empty. form.txt is in the same directory and has 777.

Since I cannot find any problem with the script (there are no error messages in the apache log file), I am wondering if there is something wrong with the php on this server (there are also no entries in syslog). phpinfo page displays fine, and php --version tells me 8.2.1 is running.

I then changed action in form to "form.php" and added method=POST to test with separate script. form.php was simply:

<?php
        echo "NAME:";
        echo $username;
        echo "EMAIL:";
        echo $email;
        echo "MESSAGE:";
        echo $message;

        echo "POSTNAME:";
        echo $_POST['username'];
        echo "POSTEMAIL:";
        echo $_POST['email'];
        echo "POSTMESSAGE:";
        echo $_POST['message'];
?>

I did not get any output for the first three entries. But I got output for the last three entries. I expected to get output for all - since the form, by sending the inputs, should automatically create/define those variables. Am I wrong?

I then added this to form.php (in front of the code mentioned above) in order to define the variables:

 if(isset($_POST['submit']))
 {
        //fetch form data
        $username = $_POST['username'];
        $email = $_POST['email'];
        $message = $_POST['message'];
 }

because I thought defining them would solve the problem. It did not. When executing form.php (by sending the form in the html), apache.log now tells me that $username, $email and $message are not defined. But I just defined them.... ärx

CodePudding user response:

Remove the last dot (.) from filename form.txt.

WRONG FILE NAME form.txt.

$file=fopen("form.txt.", "a");

CORRECT FILE NAME

$file=fopen("form.txt", "a");

Now all entries will go to form.txt file.

Full Code is shared here:

  1. Add form method as POST and add name='submit' to submit button

     <form action=""  method="post">
     <div >
         <input name="username" type="text"  placeholder="Name">
     </div>
    
     <div >
         <input name="email" type="text"  placeholder="Email">
     </div>
     <div >
         <textarea rows="5" name="message"  placeholder="Message"></textarea>
     </div>
     <div >
         <input type="submit"  value="Save" name="submit">
     </div>
     </form>
    
  2. If form is submitted then write to file. Also you have typo mistake in frwite which is corrected too here.

     if(isset($_POST['submit'])){
    
     extract($_REQUEST);
    
     $file=fopen("form.txt", "a");
     fwrite($file, "----");
     fwrite($file," name :");
     fwrite($file, $username ."\n");
     fwrite($file," email :");
     fwrite($file, $email ."\n");
     fwrite($file," message .:");
     fwrite($file, $message ."\n");
     fclose($file);
     }
    

CodePudding user response:

So this solved it for me:

I added "name=submit" to the submit input in HTML. The form posts to form.php, which now looks like this:

<html>
<body>
 
<?php
 
// turn on error reporting
ini_set('error_reporting', 'on');
error_reporting(E_ALL);
 
// Remove all illegal characters from email
// $email_address = filter_var($email_address, FILTER_SANITIZE_EMAIL);
 
// check if form has been submitted
if(isset($_POST['submit']))
{
//fetch form data
$username = $_POST['username'];
$email = $_POST['email'];
$message = $_POST['message'];
 
//write form data
$fp = fopen('form.txt', 'a');
fwrite($fp, "---");
fwrite($fp,"name :");
fwrite($fp, $username ."\n");
fwrite($fp," email :");
fwrite($fp, $email ."\n");
fwrite($fp," message :");
fwrite($fp, $message ."\n");
fclose ($fp);
}
 
//show submitted data
echo "NAME:";
echo $username;
echo "<br>";
 
echo "EMAIL:";
echo $email;
echo "<br>";
 
echo "MESSAGE:";
echo $message;
echo "<br>";
 
echo "POSTNAME:";
echo $_POST['username'];
echo "<br>";
 
echo "POSTEMAIL:";
echo $_POST['email'];
echo "<br>";
 
echo "POSTMESSAGE:";
echo $_POST['message'];
echo "<br>";
 
 
// extract($_REQUEST); might need to be added
?>
The data has been submitted - thanks
</body>
</html>

For some reason, the extract command seems unnecessary; it works fine without it, but I am not sure why. Anyhow, the data is now processed into the file. I got it. Thanks so much, everyone!

  • Related