Jump to content


Photo

Email validation problem --unresolved--


  • Please log in to reply
3 replies to this topic

#1 Ricklord

Ricklord
  • Members
  • PipPipPip
  • Advanced Member
  • 60 posts

Posted 17 August 2006 - 10:47 AM

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.

<?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"; 
} 
?>

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

#2 Ricklord

Ricklord
  • Members
  • PipPipPip
  • Advanced Member
  • 60 posts

Posted 17 August 2006 - 10:50 AM

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

#3 Ricklord

Ricklord
  • Members
  • PipPipPip
  • Advanced Member
  • 60 posts

Posted 19 August 2006 - 11:24 AM

anyone got any suggestions on this one?

cheers
Rick

#4 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 19 August 2006 - 12:16 PM

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:

...
$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

Legend has it that reading the manual never killed anyone.
My site




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users