Page 1 of 1

Newb - Questions regarding how to do email validation

Posted: Mon Nov 05, 2007 6:23 pm
by nein
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hi,

I am very new to PHP, and am only about three weeks or so into actually learning it. I have constructed a form and it does all work except for two things which hopefully some of you may be able to assist me with.

1) First, I am try to validate an email address. I have searched the internet, and found the code in which to do it, but I am having issues actually implementing it. I was hoping some of you may be able to assist me and take a look at my code below to see where I am going wrong.

2) When an error occurs in the form, the user is sent to the formhandler.php which provides them with the errors, however, since my page is a one page portfolio they are redirected to the top of the page, but my form is at the bottom. I used the <meta http-equiv="refresh" content="0; url=index.html"/> code, and it produces the results I want to a certain extent. The problem is, if you try to reload the page, it is sent into a loop of death in which the page continously refreshes itself. I am trying to use the header option but obviously this not working because its in the formhandler.php. I tried the ob_start but could not get it to work either.

Hopefully someone will be able to shed some light on my dilemmas. Here is the code I am dealing with (I have pasted the entire php section to ensure I didn't leave any small detail out):

Code: Select all

<?php

  $name = $_POST['realname'] ;
  $email = $_POST['email'] ;
  $message = $_POST['message'] ;
  $errorFlag = false;
  $errorEmail = false;
  $errorName = false;
  $errorMessage = false;

  if (!$name) {$errorFlag = true; $errorName = true;}
  if (!$email) {$errorFlag = true; $errorEmail = true;}
  if (!$message) {$errorFlag = true; $errorMessage = true;}
  
  
  if ($errorFlag == true) {
  
?>

	<div id="errorColumn">
    <h4><img src="images/alert.gif" alt="Alert" title="Alert" class="alert"/>An error has occured:</h4>
    <ul class="ulerror">
    <?php header( "Location: formhandler.php#contactnav" ); ?>
    <?php if ($errorName == true) {echo "<li>The following fields are required: Name</li>";} ?>
    
    <?php if (eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email)) {
	echo "<li>The following fields are required: Valid Email</li>";}  ?>
    
    <?php if ($errorEmail == true) {echo "<li>The following fields are required: Email</li>";} ?>
    <?php if ($errorMessage == true) {echo "<li>The following fields are required: Message</li>";} ?>
    </ul>
    </div>

<?php } else { header( "Location: formhandler.php#contactnav" ); ?>


 <?php mail( "jamespedrazzini@gmail.com", "Pedrazzini Design Form Submission", $message, "From: $name $email" );
 	 ?>

	<div id="approvedColumn">
    <h4><img src="images/success.gif" alt="Success" title="Success" class="alert"/>Submission successful!</h4>
	<p class="lastpara">Thank you for your form submission. Sit back and relax, your inquiry will be looked into shortly.</p>
    </div>
    
    
    
    <?php } ?>
Thanks,

James


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Mon Nov 05, 2007 6:30 pm
by feyd
Where's the code you are attempting to use to validate the email address?

Have you considered using a third party library built for sending email?

Posted: Mon Nov 05, 2007 6:42 pm
by nein
I did not realize I had to actually verify the address. I assumed that catching the error was fine and I did not need to validate it. Is this a requirement?

I used to use a third party form mailer but decided to attempt to make my own.

EDIT: I had edited my code to show the verification - I guess it got mixed up. This is what my code looks like currently.

Posted: Mon Nov 05, 2007 6:47 pm
by feyd
You actually aren't catching "bad" email addresses. Sure, you output an "error" but the code continues to execute as it it didn't happen.

Also, the regular expression you found is flawed. It will error on addresses which are quite valid.

Posted: Mon Nov 05, 2007 6:58 pm
by nein
feyd wrote:You actually aren't catching "bad" email addresses. Sure, you output an "error" but the code continues to execute as it it didn't happen.

Also, the regular expression you found is flawed. It will error on addresses which are quite valid.
Interesting, thanks for the input! Do you have any possibly solutions that you would recommend me looking into?

Posted: Mon Nov 05, 2007 7:08 pm
by feyd
Firstly, I would recommend using a library specifically built for sending email. They are purpose-built and have had many many hours put into them to make sure they send mail properly. We recommend Swift Mailer around here due to one of our own community making it and that it performs quite nicely.