I have a simple php script that allows users to submit their email addresses via a form on an html page. It writes their email addresses to a simple txt file. The problem is I need to add a step to take them to a styled error page if they don’t enter their email address, or if their email address is entered in an invalid format.
Here is the current code:
[php]<?php
$email = $_POST[‘email’];
$email = htmlspecialchars($_POST[‘email’]);
$file = fopen(“mailing.txt”, “a”);
fwrite($file, “\n” . $email);
fclose($file);
header(“Location: mailing_thankyou.html”);
?>[/php]
I want to add in a statement to test for email format, and if it is invalid, send them to a page called error.html. The below statement tests for the email entry, but it takes invalid entries to an unstyled php page.
[php]$email = htmlspecialchars($_POST[‘email’]);
if (!preg_match("/([\w-]+@[\w-]+.[\w-]+)/",$email))
{
die (“Location: error.html”);
}[/php]
The entire code currently looks like this:
[php]<?php
$email = $_POST[‘email’];
$email = htmlspecialchars($_POST[‘email’]);
if (!preg_match("/([\w-]+@[\w-]+.[\w-]+)/",$email))
{
die (“Location: error.html”);
}
$file = fopen(“mailing.txt”, “a”);
fwrite($file, “\n” . $email);
fclose($file);
header(“Location: mailing_thankyou.html”);
?>[/php]
Does anyone know how I can redirect invalid or entries to “error.html” rather than using the “die” function to take them to an unstyled error message.
Any assistance would be greatly appreciated.