I am using the following code to save the e-mails entered in the form. But on clicking on "Submit", it is giving an internal server error. Please help in rectifying it.
Code for form.php:
<html>
<body>
<form action="email_script.php" method="POST">
<p> Email Address: <input type="text" name="email" size="30"/> </p>
<input type="submit" name="submit" value="Submit"/>
</body>
</html>
Code for email_script.php:
<? php
$email = $_POST('email');
$file = "file.html"
file_put_contents($file, $email . PHP_EQL, FILE_APPEND);
print("...");
?>
1 Answer 1
You have a gap between <? and php. This should be <?php.
Also, $email = $_POST('email'); should be $email = $_POST['email']; (with square brackets)
Also, as pointed out below, the second line needs a semi colon at the end.
Also, I have never heard of PHP_EQL. Perhaps you mean PHP_EOL?
If you are working in a local environment, consider putting error_reporting(-1); at the top of your php file (after the <?php). This will give more detailed errors than just 'Internal Server Error'. BUT REMEMBER to remove the line before putting the script in a live environment.
ALSO - be aware that just adding data from a form into a file on the server, without validating or cleaning, is very very dangerous. I won't go into more detail as that's not what your question asked, but maybe look into that a little as well.