Jump to content


Photo

integrating send mail form with email validation


  • Please log in to reply
5 replies to this topic

#1 drfill

drfill
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 13 August 2006 - 11:37 PM

Hello

I am somewhat of a newbie when it comes to php, so I appreciate this question is probably very elementary.

I currently have a form

$headersep = (!isset( $uself ) || ($uself == 0)) ? "\r\n" : "\n" ;
$name = $_POST['name'] ;
$from = $_POST['from'] ;
$location = $_POST['location'] ;
$interested = $_POST['interested'] ;
$budget = $_POST['budget'] ;
$distance = $_POST['distance'] ;
$progress = $_POST['progress'] ;
$comments = $_POST['comments'] ;
$http_referrer = getenv( "HTTP_REFERER" );
  
if (!isset($_POST['from'])) {
	header( "Location: $formurl" );
	exit ;
}
if (empty($name) || empty($from)) {
   header( "Location: $errorurl" );
   exit ;
}

if ( ereg( "[\r\n]", $name ) || ereg( "[\r\n]", $from ) ) {
	header( "Location: $errorurl" );
	exit ;
}

if (get_magic_quotes_gpc()) {
	$comments = stripslashes( $comments );
}


$messageproper =

	"Enquiry from:\n" .

etc that is a regular run of the mill 'send a email from form' script

However, I get a lot of people entering wrong email addresses, e.g. using commas, not putting a suffix on, etc - so I'm wanting to be able to validate that the email address entered is in the correct format.

So far I've tried several bits of code out, e.g.

function valid_email($from)
{
  // check an email address is possibly valid
  if (ereg('^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+.[a-zA-Z0-9-.]+$', $from))
   return true;
  else 
	header( "Location: $errorurl" );
	exit ; 
}

but they all return one error or another. Unfortunately given my very basic knowledge of how php works I can't integrate the latter code with the above code. If anyone can give me any pointers on how to do this it would be greatly appreciated.

#2 willpower

willpower
  • Members
  • PipPipPip
  • Advanced Member
  • 296 posts
  • LocationWarrington, Cheshire, UK

Posted 14 August 2006 - 12:07 AM

post your complete code with the integration of the validation....it may just be in the wrong place.  Then we can solve your issue
Wait...I've got it...Bollocks, it's gone again!

If at first you don't succeed....stop using ASP and get onto PHP

#3 drfill

drfill
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 14 August 2006 - 01:36 AM

ok here it is

<?

$mailto = email@address.com;
$subject = "Feedback" ;
$formurl = "index.html" ;
$errorurl = "application-error.html" ;
$thankyouurl = "_application.html" ;

$uself = 0;
 
$headersep = (!isset( $uself ) || ($uself == 0)) ? "\r\n" : "\n" ;
$name = $_POST['name'] ;
$from = $_POST['from'] ;
$location = $_POST['location'] ;
$http_referrer = getenv( "HTTP_REFERER" );
  
if (!isset($_POST['from'])) {
	header( "Location: $formurl" );
	exit ;
}
if (empty($name) || empty($from)) {
   header( "Location: $errorurl" );
   exit ;
}

function valid_email($from)
{
  // check an email address is possibly valid
  if (ereg('^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+.[a-zA-Z0-9-.]+$', $from))
   return true;
  else 
	header( "Location: $errorurl" );
	exit ; 
}

if ( ereg( "[\r\n]", $name ) || ereg( "[\r\n]", $from ) ) {
	header( "Location: $errorurl" );
	exit ;
}

if (get_magic_quotes_gpc()) {
	$comments = stripslashes( $comments );
}


$messageproper =

	"Enquiry from:\n" .
	"$http_referrer\n" .
	"------------------------------------------------------------\n" .
	"Name: $name\n" .
	"Email: $from\n" .
	"Location: $location\n" .
	"Interested in: $interested\n" .
	"\n\n------------------------------------------------------------\n" ;

mail($mailto, $subject, $messageproper,
	"From: \"$from\" <$from>" . $headersep . "Return-Path: \"$name\" <$from>" . $headersep . "Reply-To: \"$from\" <$from>" . $headersep . "X-Mailer: chfeedback.php 2.07" );
header( "Location: $thankyouurl" );
exit ;

?>


as you can see, the code is just sitting there. I've tried it in a few different places, but nought works

#4 DylanBlitz

DylanBlitz
  • Members
  • PipPipPip
  • Advanced Member
  • 99 posts
  • LocationOC Baby!

Posted 14 August 2006 - 01:58 AM

Since the email verification is a function you have to call it. Functions don't do anything unless you say hey, I need you to work! :)

$mailto = email@address.com;
$subject = "Feedback" ;
$formurl = "index.html" ;
$errorurl = "application-error.html" ;
$thankyouurl = "_application.html" ;

$uself = 0;
 
$headersep = (!isset( $uself ) || ($uself == 0)) ? "\r\n" : "\n" ;
$name = $_POST['name'] ;
$from = $_POST['from'] ;
$location = $_POST['location'] ;
$http_referrer = getenv( "HTTP_REFERER" );
  
if (!isset($_POST['from'])) {
	header( "Location: $formurl" );
	exit ;
}
if (empty($name) || empty($from)) {
   header( "Location: $errorurl" );
   exit ;
}

function valid_email($from)
{
  // check an email address is possibly valid
  if (ereg('^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+.[a-zA-Z0-9-.]+$', $from))
   return true;
  else 
	header( "Location: $errorurl" );
	exit ; 
}

$email_err = valid_email($from);

if ( ereg( "[\r\n]", $name ) || ereg( "[\r\n]", $from ) ) {
	header( "Location: $errorurl" );
	exit ;
}

if (get_magic_quotes_gpc()) {
	$comments = stripslashes( $comments );
}


$messageproper =

	"Enquiry from:\n" .
	"$http_referrer\n" .
	"------------------------------------------------------------\n" .
	"Name: $name\n" .
	"Email: $from\n" .
	"Location: $location\n" .
	"Interested in: $interested\n" .
	"\n\n------------------------------------------------------------\n" ;

mail($mailto, $subject, $messageproper,
	"From: \"$from\" <$from>" . $headersep . "Return-Path: \"$name\" <$from>" . $headersep . "Reply-To: \"$from\" <$from>" . $headersep . "X-Mailer: chfeedback.php 2.07" );
header( "Location: $thankyouurl" );
exit ;


#5 drfill

drfill
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 14 August 2006 - 02:01 AM

sorry,  I should clarify and say that the form works perfectly until I add the extra code into it, and thats when things fall apart.

#6 DylanBlitz

DylanBlitz
  • Members
  • PipPipPip
  • Advanced Member
  • 99 posts
  • LocationOC Baby!

Posted 14 August 2006 - 03:20 AM

ok, what does it do when you add the extra code?




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users