0

Here's the form:

<form>
    <form action="" method="post">
    <input type="text" id="name" name="name" placeholder="Name">
    <input type="text" id="message" name="message" 
placeholder="Grateful for?">
    <br>
    <br>
    <input type=submit value="&#10086;">
    <input type="reset" value="&#x2716;">
</form>

Here's the code:

<?php
if(isset($_POST['submit'])){
$Name = $_POST['name'];
$Message = $_POST['message'];
$file = fopen("post.txt", "a");
fwrite($file, $Name, $Message);

fclose($file);

}
?>

Form is asking for input of a 'name' and a short 'message', those values should be available with PHP $_POST superglobal. Is my syntax correct with the "$Name" or "Message" variable associated with the $_POST variable?

The $file variable looks correctly set to my post.txt and am I writing out correct syntax for fwrite and fclose?

1 Answers1

0

In order to receive $_POST['submit'], you need to have a field with name="submit".

You can set that name to your submit button:

<input name="submit" type="submit" value="&#10086;">

Also, keep in mind that fwrite() has the following signature:

fwrite ( resource $handle , string $string [, int $length ] ) : int

You can simplify your code with file_put_contents() though:

file_put_contents('post.txt', "$Name\n$Message\n", FILE_APPEND);

I'm assuming you want to have your name and message separated by new line character in the file and you want to append to the file.

Robo Robok
  • 21,132
  • 17
  • 68
  • 126