Jump to content

Archived

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

drfill

integrating send mail form with email validation

Recommended Posts

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

[code]$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" .[/code]

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.

[code]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 ;
}[/code]

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.

Share this post


Link to post
Share on other sites
post your complete code with the integration of the validation....it may just be in the wrong place.  Then we can solve your issue

Share this post


Link to post
Share on other sites
ok here it is

[code]
<?

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

?>

[/code]

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

Share this post


Link to post
Share on other sites
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! :)

[code]
$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 ;[/code]

Share this post


Link to post
Share on other sites
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.

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.