"Contact Us" Form

I have created a simple contact form on my site <snip/>

The form code in the web page is:

<form action=“includes/mail.php” method=“POST”>
<p style=“color: rgb(27,161,226); font-weight: bold; font-family: calibri;”>Name</p>
<input name=“name” type=“text”>
<p style=“color: rgb(27,161,226); font-weight: bold; font-family: calibri;”>Email</p>
<input name=“email” type=“text”>
<p style=“color: rgb(27,161,226); font-weight: bold; font-family: calibri;”>Message</p>
<textarea name=“message” rows=“8” cols=“40”></textarea><br>
<input value=“Send” type=“submit”><input value=“Clear” type=“reset”>
</form>

The content of mail.php is:

<!–?php $name = $_POST[‘name’];
$email = $_POST[‘email’];$message = $_POST[‘message’];
$formcontent=“From: $name
Message: $message”;$recipient = “enquiries@la-matha.com”;
$subject = “Contact Form”;$mailheader = "From: $email \r
“;
mail($recipient, $subject, $formcontent, $mailheader) or die(“Error!”);echo “Thank You!” . " -” . “<a href=‘…/contact2.php’ style=‘text-decoration:none;color:rgb(27,161,226);’–>
<html>
<head>
<meta content=“text/html; charset=windows-1252” http-equiv=“content-type”>
</head>
<body>Return to La Matha site”;
?>
</body>
</html>

I would like to miss the echo page out, and have the user go straight to contact2.php when pressing “send”,

Forgive what is probably a basic and simple request, but I am a novice with no background in programming etc.

Denis

Hi Denis,

You can redirect the user directly to contact2.php using the [fphp]header[/fphp] function, like this:

header('Location: http://www.la-matha.com/contact2.php');

It’s important to note that you must call the function before outputting anything to the screen, otherwise you’ll get an error.

Here’s the full example of your mail.php page with the page redirection code added. I’ve also added some basic filtering of your form data, which is always a good idea when you’re dealing with input from users. Although I’ve not shown it here, you might also want to check that the email address supplied is valid, and $message isn’t empty before you send the email.


<?php
$name = filter_input(INPUT_POST, 'name', FILTER_SANITIZE_SPECIAL_CHARS);
$email = filter_input(INPUT_POST, 'email', FILTER_SANITIZE_EMAIL);
$message = filter_input(INPUT_POST, 'message', FILTER_SANITIZE_SPECIAL_CHARS);

$formcontent = "From: $name \
 Message: $message";
$recipient = "enquiries@la-matha.com";
$subject = "Contact Form";
$mailheader = "From: $email \\r\
";

if ( mail($recipient, $subject, $formcontent, $mailheader) ){
    header('Location: http://www.la-matha.com/contact2.php');
    exit;
} else {
    // Would be better to present some kind of friendly error for your users!
    die("Error!");
}

Thanks for the help - it works exactly as I want it to. I’m not going to bother with validation - to be honest, I don’t expect much traffic, but I wanted something that looked professional.

Help from sitepoint is always appreciated!

Denis

you can redirect to the user page using header function but it behave differently on each server and its not worked in some server so another way is,

$targetPage ="http://www.la-matha.com/contact2.php";
<script language="javascript">
            window.location.href="<?php echo $targetPage;?>";
        </script>

Can you explain what you mean? I’ve never had a problem using the header function to do a redirect… the location header is part of the HTTP spec and should work across all browsers.

I’d avoid using JS to do a redirection if possible because obviously it’s going to break if the user has JS disabled.

I am not sure about that, while i used header function it shows error “Cannot modify header information - headers already sent by (output started at… )” . What is reason its shows this error?

The header function has to be called before any output is sent to the browser, otherwise you get the error message you describe.


// This will trigger a "headers already sent" error
echo "<p>Thank you for your email</p>";
header('Location: http://www.example.com'); 


// So will this, because of the space before the opening PHP tag
 <?php
header('Content-type: application/json');

oh k, Thanks lot