PHP Email Confirmation!?!

Hello,
I know this is a basic question but I can seem to get this contact form to verify an email address. I’m trying to set it up so that the user has to enter in all the fields that’s all. Currently if all fields are filled it will send you to clrsvp.php and say thank you then redirect to the home page. If the email field is left blank then it will take you to clrsvp.php but only a blank page that wont send the email or redirect. Any help is much appreciated.
Thank you.

Here is the php:
[php]<?php

// first clean up the input values
foreach($_POST as $key => $value) {
if(ini_get(‘magic_quotes_gpc’))
$_POST[$key] = stripslashes($_POST[$key]);

$_POST[$key] = htmlspecialchars(strip_tags($_POST[$key]));
}

$ip=$_SERVER[‘REMOTE_ADDR’];
$email_to = "[email protected]";
$email_subject = “”;

$email_message .= “Name Entered: “.$_POST[“fname”].”\n”;
$email_message .= “Email: “.$_POST[“lname”].”\n”;
$email_message .= “Confirm Email: “.$_POST[“email”].”\n”;

$userEmail = filter_var( $_POST[‘email’],FILTER_VALIDATE_EMAIL );

if(!$userEmail ){
exit;
}

$email_message .= “Invitation Code: “.$_POST[“email1”].”\n”;

//email headers
$headers = 'From: '.$_POST[“email”]."\r\n".
'Reply-To: '.$_POST[“email”]."\r\n" .
‘X-Mailer: PHP/’ . phpversion();

echo (mail($email_to, $email_subject, $email_message, $headers) ? "

Please return to .


":“

We’re sorry, something went wrong.

Please return to .

”);

$ip=$_SERVER[‘REMOTE_ADDR’];
$email_to = $_POST[“email”];
$email_subject = "RSVP | ";

$email_message1 = "Thank you you have RSVP’d!

Hello and welcome to !

Sincerely,
The Team
";

//email headers
$headers = 'From: '.$_POST[“email”]."\r\n".
'Reply-To: '.$_POST[“email”]."\r\n" .
‘X-Mailer: PHP/’ . phpversion();

echo (mail($email_to, $email_subject, $email_message1, $headers) ? “”:"");
die();[/php]

Here is the HTML:

[php]

Name:

Email Address:

Confirm Email:

Invitation Code:


[/php]

First, you should use better identifier names. You’ve got two email fields, a normal one and then a confirmation field.

Your first one is named “lname”, typically standing for last name, and the second one is “email”. I’d switch them to “email” and “cemail” or “confirmemail”.

I’m not really sure what this is supposed to be doing:
[php] if( ! $userEmail ){

  exit;

}[/php]

But, if you change it to:
[php] if (!empty ($userEmail)) {
echo = “Sorry, you did not enter an email address.”;
}[/php]

I think it should work.

Actually, just saw this on stackoverflow…pretty nice:
[php]// Required field names
$required = array(‘login’, ‘password’, ‘confirm’, ‘name’, ‘phone’, ‘email’);

// Loop over field names, make sure each one exists and is not empty
$error = false;
foreach($required as $field) {
if (empty($_POST[$field])) {
$error = true;
}
}

if ($error) {
echo “All fields are required.”;
} else {
echo “Proceed…”;
}[/php]

Thank you,
But with this code I get an error on the echo

[php]if (!empty ($userEmail)) {
echo = “Sorry, you did not enter an email address.”;
}[/php]

I added the code and fixed the name but when there is nothing entered I get a successful page. When I do type in the fields I get a sucessful page and it works. How can I get it to not send if the fields are empty? Even if it does nothing but not click the submit until there is text in the fields.

Here is the current code:
[php]<?php

// first clean up the input values
foreach($_POST as $key => $value) {
if(ini_get(‘magic_quotes_gpc’))
$_POST[$key] = stripslashes($_POST[$key]);

$_POST[$key] = htmlspecialchars(strip_tags($_POST[$key]));
}

$ip=$_SERVER[‘REMOTE_ADDR’];
$email_to = “[email protected]”;
$email_subject = “”;

$email_message .= “Name Entered: “.$_POST[“name”].”\n”;

$email_message .= “Email: “.$_POST[“email”].”\n”;

$email_message .= “Confirm Email: “.$_POST[“cemail”].”\n”;

$userEmail = filter_var( $_POST[‘cemail’],FILTER_VALIDATE_EMAIL );

$email_message .= “Invitation Code: “.$_POST[“invite”].”\n”;

//email headers

$headers = 'From: '.$_POST[“email”]."\r\n".

'Reply-To: '.$_POST[“email”]."\r\n" .

‘X-Mailer: PHP/’ . phpversion();

echo (mail($email_to, $email_subject, $email_message, $headers) ? "

Thank you, you have successfully !

Please return to .


":“

We’re sorry, something went wrong.

Please return to .

”);

$ip=$_SERVER[‘REMOTE_ADDR’];
$email_to = $_POST[“email”];
$email_subject = "RSVP | ";

$email_message1 = "Thank you you have RSVP’d fo!

Hello and welcome to !

You have now been RSVP’d for the .

Please bring:

Sincerely,
The Team
";

//email headers

$headers = 'From: '.$_POST[“email”]."\r\n".

'Reply-To: '.$_POST[“email”]."\r\n" .

‘X-Mailer: PHP/’ . phpversion();

echo (mail($email_to, $email_subject, $email_message1, $headers) ? “”:"");

// Required field names
$required = array(‘name’, ‘email’, ‘cemail’, ‘invite’);

// Loop over field names, make sure each one exists and is not empty
$error = false;
foreach($required as $field) {
if (empty($_POST[$field])) {
$error = true;
}
}

if ($error) {
echo “All fields are required.”;
} else {
echo “Proceed…”;
}

?>[/php]

Hmmm, odd. I’ll admit I’m not much of a PHP expert, so if I can’t help you, someone else definitely will. What was the error you received?

You could try using JavaScript for validation instead of PHP. Might be easier.

Try using http://jsfiddle.net/CrLsR/8/ as a template.

More specifically, look at:

// Validate Email var email = $("#fremail").val(); if ((/(.+)@(.+){2,}\.(.+){2,}/.test(email)) || email=="" || email==null) { } else { alert("Please enter a valid email"); }

And if you decide to go that route, make sure to put this as a form attribute:

onsubmit="return validateForm();"

Thank you for the help awl19,
The template I’m working with is very picky on what will and wont work. Ill give the JS a try and see if that works.
Thank you again.

Remember that as a developer/site owner you can not trust data coming from the user.

Validation done in javascript is excellent for quickly showing the user there is something wrong with his inputted data. You can not trust the data received server side to be validated though, so you should always validate all data server side as well.

I’m sorry i couldn’t be of more use at actually solving the problem, too late now where I am (writing this on my phone)

Awl19,

The reason why the following code doesn’t work is because your code states that if there is NO EMPTY variable of $userEmail, echo the error. This would have been the correct code:

[php]if (empty ($userEmail)) {
echo = “Sorry, you did not enter an email address.”;
}[/php]

I took out the ! from in front of empty so it should now translate to: if variable $userEmail is empty, echo out the error message.

Cheers!

Hey Guys,
I found a huge part of the problem. In the form I had the form action referral.php but in order to load within the index.html page I had to add #!referral.php. But with that being changed it will show the error message but wont fill in the fields. I receive a blank email that just says…

Name Entered:
Email:
Confirm Email:
Invitation Code:

I also tried that edited code without the ! but in DreamWeaver I recieve an error because of the whole echo tag. With out the echo tag and just “if (empty ($userEmail)) {}” seems to have no errors.

HTML:
[php]

REFERAL SIGN UP


Full Name:

Email Address:

Confirm Email:


        

[/php]

PHP
[php]<?php

// first clean up the input values
foreach($_POST as $key => $value) {
if(ini_get(‘magic_quotes_gpc’))
$_POST[$key] = stripslashes($_POST[$key]);

$_POST[$key] = htmlspecialchars(strip_tags($_POST[$key]));
}

$ip=$_SERVER[‘REMOTE_ADDR’];
$email_to = “[email protected]”;
$email_subject = "REFERRAL SIGN UP | ";

$email_message .= “Name Entered: “.$_POST[“name”].”\n”;

$email_message .= “Email: “.$_POST[“email”].”\n”;

$email_message .= “Confirm Email: “.$_POST[“cemail”].”\n”;

$userEmail = filter_var( $_POST[‘cemail’],FILTER_VALIDATE_EMAIL );

if (empty ($userEmail)) {}

$email_message .= “Invitation Code: “.$_POST[“invite”].”\n”;

//email headers

$headers = 'From: '.$_POST[“cemail”]."\r\n".

'Reply-To: '.$_POST[“cemail”]."\r\n" .

‘X-Mailer: PHP/’ . phpversion();

echo (mail($email_to, $email_subject, $email_message, $headers) ? "

Thank you, you have successfully signed up for 'S !

Please return to .


":“

We’re sorry, something went wrong.

Please return to .

”);

$ip=$_SERVER[‘REMOTE_ADDR’];
$email_to = $_POST[“cemail”];
$email_subject = "| ";

$email_message1 = "Thank you you have signed up for 'S !

Hello and welcome to !

You have now you have signed up for 'S Referral Program.

Please allow 48 hours for us to finish the sign up process and set up your referral code.

Sincerely,
The Team
";

//email headers

$headers = 'From: '.$_POST[“email”]."\r\n".

'Reply-To: '.$_POST[“email”]."\r\n" .

‘X-Mailer: PHP/’ . phpversion();

echo (mail($email_to, $email_subject, $email_message1, $headers) ? “”:"");

// Required field names
$required = array(‘name’, ‘email’, ‘cemail’, ‘invite’);

// Loop over field names, make sure each one exists and is not empty
$error = false;
foreach($required as $field) {
if (empty($_POST[$field])) {
$error = true;
}
}

if ($error) {
echo “All fields are required.”;
} else {
echo “Proceed…”;
}

?>

[/php]

I’m not certain this will change anything, but sometimes it does.

Try:
[php] if (!empty ($userEmail)) {
echo = ‘Sorry, you did not enter an email address.’;
}[/php]

It’s single quotes instead of double quotes.

And I noticed that for all your $_POST fields, you have double quotes except for:
[php]$userEmail = filter_var( $_POST[‘cemail’],FILTER_VALIDATE_EMAIL );[/php]
I believe cemail should have double quotes.

This is my limited knowledge on the usage of single and double quotes.
Single quotes are used for echoes and other functions where the output is…I guess I’d say static. Unchanging.
Double quotes are used for variables and dynamic functions.
Someone correct me if that’s wrong.

Also, I’d get rid of the code from 103 to 119, or at least just comment it out for now because while it’s good code, it would be best to wait on that till after we get the root issue fixed. You can’t use both.

This doesn’t look right. If $userEmail is not empty, then show an error saying it is empty?

It does not matter if you use single or double quotes. There are some different rules when it comes to quotes and using strings/variables, but as long as you concatenate the variables php doesn’t care which you use.

I use only single quotes server side as I prefer not to mix standards, and in my opinion single quotes offer better readability because you are forced to contatenate.

Ex:
[php]echo ‘In this string it’s kinda $hidden that we’ve included a variable.’;

echo ‘In this string however it ’ . $shows . ’ pretty well’;[/php]

You have two different forms, one referral form and one clrsvp form, they have the following field names:

[code]referral form
name

clrsvp form
fname
lname
email
email1[/code]

This thread started out with the clrsvp form / server code, but in your last post you introduced this referral form. Which one is it you’re having problems with?

[hr]

clrsvp form

The form field names are all wrong.
Name = fname
Email Address = lname
Confirm Email = email
Invitation Code = email1

[php]

  <tr>
     <td valign="top">
        <label for="fname"><p>Name:</p></label></td>
     <td valign="top">
        <input  type="text" name="fname" maxlength="50" size="30" style="color:#000;">
     </td>
  </tr>
  <tr>
     <td valign="top">
        <label for="lname"><p>Email Address: </p></label>
     </td>
     <td valign="top">
        <input  type="text" name="lname" maxlength="50" size="30" style="color:#000;">
     </td>
  </tr>
  <tr>
     <td valign="top">
        <label for="email"><p>Confirm Email: </p></label></td>
     <td valign="top">
        <input  type="text" name="email" maxlength="80" size="30" style="color:#000;">
     </td>
  </tr>
  <tr>
     <td valign="top">
        <label for="email1"><p>Invitation Code:</p></label></td>
     <td valign="top">
        <input  type="text" name="email1" maxlength="30" size="30" style="color:#000;">
     </td>
  </tr>

  <tr>
     <td colspan="2" style="text-align:center"><br />
        <input type="submit" value="RSVP" style="color:#000;">
     </td>
  </tr>
[/php]

fixed:

[php]

  <tr>
     <td valign="top">
        <label for="fname"><p>Name:</p></label></td>
     <td valign="top">
        <input  type="text" name="name" maxlength="50" size="30" style="color:#000;">
     </td>
  </tr>
  <tr>
     <td valign="top">
        <label for="lname"><p>Email Address: </p></label>
     </td>
     <td valign="top">
        <input  type="text" name="email" maxlength="50" size="30" style="color:#000;">
     </td>
  </tr>
  <tr>
     <td valign="top">
        <label for="email"><p>Confirm Email: </p></label></td>
     <td valign="top">
        <input  type="text" name="cemail" maxlength="80" size="30" style="color:#000;">
     </td>
  </tr>
  <tr>
     <td valign="top">
        <label for="email1"><p>Invitation Code:</p></label></td>
     <td valign="top">
        <input  type="text" name="invite" maxlength="30" size="30" style="color:#000;">
     </td>
  </tr>

  <tr>
     <td colspan="2" style="text-align:center"><br />
        <input type="submit" value="RSVP" style="color:#000;">
     </td>
  </tr>
[/php]

[hr]

clrsvp.php - I think this is what you’re after

[php]<?php

// first clean up the input values
foreach ($_POST as $key => $value) {
if (ini_get(‘magic_quotes_gpc’)) {
$_POST[$key] = stripslashes($_POST[$key]);
}
$_POST[$key] = htmlspecialchars(strip_tags($_POST[$key]));
}

// Required field names
$required = array(‘name’, ‘email’, ‘cemail’, ‘invite’);

// Loop over field names, make sure each one exists and is not empty
$error = false;
foreach ($required as $field) {
if (empty($_POST[$field])) {
$error = true;
}
}

if ($error) {
echo ‘All fields are required.’;

} else if ($_POST[‘email’] !== $_POST[‘cemail’]) {
echo ‘Email and email confirmation are not equal.’;

} else {
$userEmail = filter_var($_POST[‘email’], FILTER_VALIDATE_EMAIL);
$userConfirmEmail = filter_var($_POST[‘cemail’], FILTER_VALIDATE_EMAIL);

// send mail to user
$headers = 'From: ’ . ‘[email protected]’ . “\r\n” .
'Reply-To: ’ . ‘[email protected]’ . “\r\n” .
‘X-Mailer: PHP/’ . phpversion();

$email_message = "Thank you you have signed up for 'S !

 Hello and welcome to ! 

 You have now you have signed up for 'S Referral Program.

 Please allow 48 hours for us to finish the sign up process and set up your referral code.

 Sincerely,
 The Team
 ";

mail($userEmail, '| ', $email_message, $headers);

// send mail to site owner
$headers = 'From: ’ . $_POST[“cemail”] . “\r\n” .
'Reply-To: ’ . $_POST[“cemail”] . “\r\n” .
‘X-Mailer: PHP/’ . phpversion();

$email_message = 'Name Entered: ’ . $_POST[“name”] . “\n”;
$email_message .= 'Email: ’ . $userEmail . “\n”;
$email_message .= 'Confirm Email: ’ . $userConfirmEmail . “\n”;
$email_message .= "Invitation Code: " . $_POST[“invite”] . “\n”;
$email_message .= 'Sent from: ’ . $_SERVER[‘REMOTE_ADDR’] . “\n”;

mail(‘[email protected]’, 'REFERRAL SIGN UP | ', $email_message, $headers);
}[/php]

[hr]

In order to get the functionality of not sending the form before all fields are filled out (correctly) you should use javascript as well to verify the input. Look into Jquery validator :slight_smile:

Sponsor our Newsletter | Privacy Policy | Terms of Service