Jump to content

Archived

This topic is now archived and is closed to further replies.

Ricklord

Email validation problem --unresolved--

Recommended Posts

Hi Guys,

I been trying to use some tuturials to add a email validator to my current email script.

this is what ive come up with so far.

[code]<?php

header("Location: http://www.creativecogs.co.uk/web_design/thankyou.html");

if(isset($_POST['submit'])) {

$to = "rick@creativecogs.co.uk";
$subject = "Web Design Online enquiry";

$name_field = $_POST['name'];
$email_field = $_POST['email'];
$telephone_field = $_POST['telephone'];
$message = $_POST['message'];
$header = "From: {$name}<{$email_field}>";

function checkemail($email_field){ 
   return preg_match("/^[^\s()<>@,;:\"\/\[\]?=]+@\w[\w-]*(\.\w[\w-]*)*\.[a-z]{2,}$/i",$email_field); 
}

if((!$email_field) OR (!checkemail($email_field))){ echo ' - You did not enter a valid e-mail address.'; exit; }


 
foreach($_POST['check'] as $value) {
$check_msg .= "Checked: $value\n";
}
 
$body = "From: $name_field\n E-Mail: $email_field\n Telephone: $telephone_field\n $check_msg Option: $option\n Message: $message";
 
echo "Data has been submitted to $to!";
mail($to, $subject, $body, $header);
 
} else {
echo "failed please try again";
}
?> [/code]

If i take the header out the checker will follow the echo command when an email address is invalid which is what i want but i also want to forward to my thankyou page if the email is valid.

I'm not a php coder i just learn as i go, take tuturials learn what they do and then change them to fit my needs.

If some one could point out how i can correct this i would be greatful. Is there another comand to use which i can put at the end of the script to forward onto my thankyou page? from my understanding the header command has got to be at the top of the script and will be executed first so teh user would get redirected before the check is actually done in my above example.

Any help will be great

Cheers

Rick

Share this post


Link to post
Share on other sites
Sorry forgot to add this.

instead of the echo command can i use a function/command which will forward to an error page rather than printed text.

Cheers

Rick

Share this post


Link to post
Share on other sites
Rick - the header() function needs to appear before any output to the browser, not before any code.

Given that it's redundant to echo a success message and then move to a thank you page (which can just as well say 'Your message has been sent'), just rearrange parts of your code thus:

[code]
...
$body = "From: $name_field\n E-Mail: $email_field\n Telephone: $telephone_field\n $check_msg Option: $option\n Message: $message";
 
mail($to, $subject, $body, $header);
header("Location: http://www.creativecogs.co.uk/web_design/thankyou.html");
exit();// to be sure
[/code]

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.