Jump to content

Email validation problem --unresolved--


Ricklord

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
Link to comment
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]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.