MyBB Community Forums

Full Version: PHP form help
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I am using a PHP form on my site. It works perfectly, but when a user click Submit and there is an error, it just takes them to a white page with the error text that is entered in the PHP.

My question: Is there a way to make the form direct the user to an error page that I make?
Or
Is there a way to make a popup with the error text?

Here is the PHP code I am using:
<?php
##Simple & Secure PHP Contact Form ##
##By KuJoe @ www.AdminLounge.net##

session_start();

$to = "[email protected]";
$from = $_POST["email"];
$subject = $_POST["subject"];
$name = $_POST["name"];
$message = $_POST["message"];
$messagesent = "Name: $name \r\nMessage: $message \r\nSender's IP: $HTTP_SERVER_VARS[REMOTE_ADDR]";
$headers = "From: $from";
if (!preg_match("/^([a-zA-Z -])+$/",$name))
{
die('Name must consist of letters only.');
}
if (!preg_match("/^([0-9a-zA-Z\.\,\;\?\!\'\ \-])+$/",$subject))
{
die('Subject can only contain letters, numbers, and punctuation.');
}
if (!preg_match('/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i',$from))
{
die('Invalid e-mail address.');
}
if (!preg_match("/^([0-9a-zA-Z\.\,\;\?\!\'\ \-])+$/",$message))
{
die('Message can only contain letters, numbers, and punctuation.');
}

mail($to,$subject,$messagesent,$headers);

header("Location: sent.html")
?>

Any help?
Replace

die('Message can only contain letters, numbers, and punctuation.');

with something like

$errorMessage = 'Message can only contain letters, numbers, and punctuation.';

And then put $errorMessage into your template where you want your error to appear. Do this for all of the error messages.

And then you'd also have to have some kind of if statement to check that if there are no errors, THEN it redirects to sent.html.
Sorry, that doesnt help :/
I know nothing about PHP so I have no idea what I need to do.
The code I posted works fine when the user inputs the correct information, but when they do something wrong, it just shows a white page with the text that's in the die statement. I just want that text to show either in a popup, or in a page that matches my site.
You can send the user to an error page that you have made by putting:
header('Location: customerrorpage.php'); //Send the user to the error page.
exit(); // Stop the script for executing any further.
instead of:
die('Message can only contain letters, numbers, and punctuation.');

Then you can create the page. I recomend putting a 'Back' link on that page, so that the user can get back to the contact page quickly.

BTW - the php header('Location: ') works with any domain name or file/folder - e.g.
header('Location: http://google.com');
header('Location: ../../index.php');
header('Location: /files/index/images/');
All work.

Hope that helps Smile