Email - Match two fields

Hi All,

I’m just setting up a registration page, which seems to be working ok at the moment. However, i would like to add an email confirm field so that the two fields have to match before the form is submitted. I’ve attached the code i have below with the ‘Email’ and Emailconfirm’ being to two fields that would need to match. Could someone supply me with either the code i need or where i could sort out the code. I’m learning php at the moment so it has taken me a bit of time to get where i am, but hopefully this is the last hurdle to complete the form.

Many thanks,

Keith

<?php 
 $from = $_REQUEST['Email'] ;
 $email = $_REQUEST['Emailconfirm'] ;  
 $name = $_REQUEST['Name'] ;
 $Reference = $_REQUEST['Reference'] ;
 $Forname = $_REQUEST['Christian-name'] ;
 $Street = $_REQUEST['Street'] ;
 $Town = $_REQUEST['Town'] ;
 $Post = $_REQUEST['Post-code'] ;
 $country = $_REQUEST['country'] ;
 $headers = "From: $from"; 
 $subject = "*******"; 
 
 $fields = array(); 
 $fields{"Reference"} = "Reference";
 $fields{"Name"} = "Name";
 $fields{"Christian-name"} = "Christian-name";
 $fields{"Institution"} = "Institution";
 $fields{"Street"} = "Street";
 $fields{"Town"} = "Town";
 $fields{"Post-code"} = "Post-code";
 $fields{"country"} = "country";
 $fields{"Telephone"} = "Telephone";
 $fields{"Email"} = "Email";
 $to = "*******.co.uk"; 
 
 $body = "******* details:\
\
"; foreach($fields as $a => $b){ 	$body .= sprintf("%20s: %s\
",$b,$_REQUEST[$a]); } 
 
 $headers2 = "From: *******"; 
 $subject2 = "*******"; 
 $autoreply = "
Thank you for registering with the The *******. 

http://www.*******
Surname: $name
Reference number: $Reference

-------------------------------------------


*
This e-mail is confidential and intended for the addressee (s) only. It may also be privileged. If you are not the intended recipient, please notify the sender by replying to this e-mail. You should not copy this e-mail or otherwise use it for any purpose and the contents must not be disclosed to any other person.";
 
 if($from == '') {echo "You have not entered an email, please go back and try again";} 
 else { 
 if($name == '') {echo "You have not entered a name, please go back and try again";} 
 else { 
 $send = mail($to, $subject, $body, $headers); 
 $send2 = mail($from, $subject2, $autoreply, $headers2); 
 if($send) 
 {header( "Location: *******" );} 
 else 
 {echo "We encountered an error sending your mail, please notify webmaster@YourCompany.com"; } 
 }
}
 ?>
 <?php
   $headers = "From:*******".com\\r\
";
   $recipients = "*******".com";
   mail($recipients, "This is the subject","This is the mail body", $headers);
?>

If you want to compare the 2 email strings, you could first convert both to either upper or lower case to make the comparison case insensitive and then just use an IF statement to take appropriate action if the 2 email strings are not equal to each other.

Thanks for the quick response. Is there any chance of a working example of the suggestion you have made. I’m fairly new to php so an example would be of great help.

Thanks again for your help.

Something doesn’t make sense here.

If you actually are fairly new to php then you couldn’t have written the code you posted because the php code concept you need to compare the 2 emails appears in the code you posted.

So it now appears to me that you probably got your posted code from somewhere else and you now simply want someone to tweak it to getting it working for you for free.

If you want to learn how to do it, there are plenty of tutorials and code examples on the www showing you how to compare the value of 2 strings.

Thanks again for getting back to me,

I can understand why you have written that, but I have put a lot of time and effort in to getting this form to work so please don’t think i’m trying to cheat to get things done. I am new to php and of course have not written all of this from scratch. If i had i’m sure i know how to do what i have asked help for. However, i have also not just found a php file which contains all of the information i needed in a nice neat file and hope that someone else can alter it for me. It has taken me ages to put this file together. There are lots of free resources available on the internet (e.g. sitepoint!), and through lots of trial and error and a Frankenstein monster attitude i have got the form to do what i want to when i click submit. I’m sure you are just being protective over other peoples work, but if you read my first post, I thought i explained clearly that i would be happy if someone could just point me in the right direction for a tutorial. I can’t write php from scratch, but I can understand from reading tutorials how aspects work. From this perspective because i am not used to the language used in php I don’t know what to look for to find a tutorial. There seems to be lots of tutorials on how to validate an email address but can’t find any help with getting two fields tomatch.

If you are unable to help me, then that is not a problem and i do appreciate that you have taken the time to reply to me. However, hopefully you can understand form my perspective for being a bit annoyed for be accused of just trying to steal other peoples work when you have spent a long time getting to this stage.

Keith

ok, but that may or may not all be true. I just find it hard to believe you don’t how to compare 2 strings when you are already comparing strings to each other in your posted code.

Like I said, there are countless examples on the www showing how to compare 2 strings.

I am also a rank beginner. I just started trying my hand at programming PHP a few months ago.

I think what you want is something that would do this:
if a equals b then (set your variables)

I think that will look something like this:


	if (($_REQUEST['Email'] == $_REQUEST['Emailconfirm']){
	  $from = $_REQUEST['Email'] ;
		$email = $_REQUEST['Emailconfirm'] ; 
		}

I have learned that when you compare two variables, you use a pair of equal signs. If the statement is true, the script performs what is between the two {}.

If you want to have something happen if they do not patch, you could do something like this:

	if (($_REQUEST['Email'] == $_REQUEST['Emailconfirm']){
	  $from = $_REQUEST['Email'] ;
		$email = $_REQUEST['Emailconfirm'] ; 
		} else {
                echo 'your email did not match the confirmation email.';
                }

Hope this is correct and helps.

Hi CSU-Bill,

Thanks for your help. I’ve managed to get the code implemented and everything seems to be working ok now. There were a couple of bits that i had to alter, though that was probably more down to my lack of knowledge and how it ended up working.


 if ($_REQUEST['Email'] != $_REQUEST['Emailconfirm']){echo "your email did not match the confirmation email.";}
 else
{									
 if($from == '') {echo "You have not entered an email, please go back and try again";} 
 else { 
 if($name == '') {echo "You have not entered a name, please go back and try again";} 
 else { 
 
 $send = mail($to, $subject, $body, $headers); 
 $send2 = mail($from, $subject2, $autoreply, $headers2); 
 if($send) 
 {header( "Location: http://www.oddsockdesign.co.uk/rac/test/registration.html" );} 
 else 
 {echo "We encountered an error sending your mail, please notify webmaster@YourCompany.com"; } 
 }
}
}

What i found was that the other if statements seemed to work on a negative statement and the code would only proceed if that criteria was met. I changed your code to ‘!=’ which after a bit of google found out was the code for not equal to. Am very please and relived to get the last element sorted. Very mutch appreciate your help, I found once you see the pattens that are in the code it is easier to understand what your are asking the code to do.

webdev1958 - Can’t be bothered. Sitepoint is a great community that is here to ‘help people build a better web’. All i have asked in this post is for tutorials or advice as to how i can solve the initial problem set. I suggest you adopt the attitude that sitepoint outlines as it i here as a means to be ‘THE resource for web designers and developers.’ It is full of great tutorials and professionals and is a shame that you were not able to adopt that mind set.

When you tested your work, did you test leaving both email entries blank?

I am just wondering if both being blank will meet the comparison requirement and get to your blank check.

Just double checked that and it all works ok. The original code has the email field as a required enter, so it is not possible to submit the form with out this being entered.

Many thanks,

Keith

Famous last words. It’s trivial to bypass any browser side verifications. Never say “not possible” when it comes to posted data.

What [B][COLOR=#0071d8]ahundiak[/COLOR][/B] said!

Also, just because it is checking that the address is submitted, does not mean that it is checking that the two submitted addresses match. This is very messy, but you want to do something like:


$emailCheck=strtolower($emai1l);
$emailConfirm=strtolower($email2);

if ($emailCheck==$emailConfirm) {

// send the form 

} else {

// return to the form
}

What the TheOriginalH said, plus add trim() to remove lead/trailing whitespaces.


$emailCheck=trim(strtolower($email1)); 
$emailConfirm=trim(strtolower($email2));

You could even check conform to email norms on both with the use of PHPs Filter

http://php.net/manual/en/filter.examples.validation.php

Thanks for the additional advice. I guess it is one thing to get the form to work, it is then another step to put in the safety measures for additional variables. I’ll give a go at implementing these elements tomorrow, so hopefully with a bit of trial and error I should be ok.

Also as Cups mentioned, I realised i do not have anything in the php that checks that the email is a valid email address. I’ll do this at the same time as the other steps that need to be implemented (thanks for the link. I’ve found php.net to be a very helpful place for explaining how the individual elements of php works).

Thanks again for all the additional help.

Hi,

php.net is the home of PHP, and of course its world class online manual.

It is easy to find out what the many functions do, take trim for example:

Simply type in your address bar php.net/trim