Checking Two Email Addresses

I am trying to make a form where I have a place to enter an email address and then another place right below it where it gets entered again. The idea for entering the email address twice is so that if a typo is made then the two addresses won’t match and an error message will be seen when the form is submitted. I suspect this is a common thing and there is probably code already made up to do this. I think I can figure out the HTML code to put into the form, but the PHP code is still pretty new to me and I have difficulties with it. If anybody has this code and could post it for me, it would be greatly appreciated.

you could do something like this in the php form processor …

[php]if(isset($_POST[‘submit’])) {

set up an empty variable for error messages

$error = ‘’;

#etc
#the validation goes here…
if($_POST[‘email_address_1’] != $_POST[‘email_address_2’]) {

stop processing and display error…

$error .= ‘The email addresses dont match’;
}

if the error var is not empty…

if($error != ‘’) {
echo $error;
} else {

continue processing

} [/php]

We kind of expect effort. Comparing two values is a very simple process.

I know but i have gotten alot of help on these boards i just feel that i should help even when people cause an x,y issue :slight_smile:

Thanks jay7981 for the help, it is much appreciated.

I have been trying to get this to work on my own for over a week now. How much more effort would you like me to put into it before I ask for a little help? I am not ashamed to admit that PHP code is all new to me and I am not very good at it. I asked for help because I was highly frustrated after so many long hours of trying things on my own and it not working. But, if asking simple/easy questions is frowned upon, then I will quit asking. Have a nice day.

No problem, dont forget to hit that Give Karma button if and only if this helped. Also for future reference please read this post as it may help us help you in the future…
Posting Guidelines - Please read before posting

P.S.
astonecipher was commenting on the fact that there was’nt a lot of information in your original post. there are about 1000000000+1 ways to do 1 thing in php for different reasons , if we dont know the specifics of what you are trying to accomplish ie. user does this -> this is supposed to happen -> this is what happens -> this is what i have tried to fix it with -> these are the results of me trying to fix it … ect … it makes it harder for us to help you. he wasnt trying to be rude or anything.

I should rephrase. You may have been working on this for a week, but we don’t know that. We ask that you post what you have tried and what isn’t working to show that you have put effort into the problem and that you are not just expecting someone to hand you code.

[member=72428]jay7981[/member],

Depending on a button name to be submitted for a script to work will completely fail in certain circumstances. The proper error proof way is to check the request method.

[php]if ($_SERVER[‘REQUEST_METHOD’] == ‘POST’)[/php]

What you have as an empty error variable would be an array if there were more than one check being done.

For a single check this
[php] if($error != ‘’) {
echo $error;
}[/php]

Should be

[php] if($error) {
echo $error;
}[/php]

Per the manual

The if expression is evaluated to its Boolean value. If expression evaluates to TRUE, PHP will execute statement, and if it evaluates to FALSE - it'll ignore it.

For a multi-check it would be a foreach to loop over the error array.

You are absolutely correct but without any part of his code i could not anticipate how to best approach his issue, but yes your method is not only better it is indeed best practice.

If I came on a little strong, I apologize, I didn’t mean to. It’s just that I have been trying my best to learn this stuff and I had checked out four books on PHP from the local library and I have been reading so much stuff about it that after awhile it gets to look a little like Egyptian Hieroglyphics. I had to return the books now, but I am planning on going to a larger book store and buying a book or two on the subject so I will have them all the time to refer to, not just for a couple weeks at a time.

When it comes to showing my work, this may sound like I am trying to argue with you, but I don’t mean it that way. When I try something and it doesn’t work, I just delete it and try something else. I don’t save the countless number of things I have tried that didn’t work, so it isn’t all that easy to send you a copy of all my failures. If I were doing it all on paper I could FAX the papers to you, but since it is done on a computer I just delete the failures and move on. After awhile I get really discouraged and want to give up. That’s when I ask for help.

In the future I can save at least couple of my failures to show you, it will probably give everyone a good laugh. I don’t mind that, I am just trying to learn and if someone finds my screw-ups to be humorous, then at least the screw-ups were good for something.

Be mindful when you buy a book on PHP. I have two shelves full myself, but I also know what good and bad practices look like where it is far harder for a beginner to distinguish.

https://www.amazon.com/PHP-MySQL-Development-Developers-Library/dp/0321833899/ref=sr_1_7?s=books&ie=UTF8&qid=1487268468&sr=1-7&keywords=php

https://www.amazon.com/Murachs-PHP-MySQL-Joel-Murach/dp/1890774790/ref=sr_1_11?s=books&ie=UTF8&qid=1487268468&sr=1-11&keywords=php

https://www.amazon.com/Objects-Patterns-Practice-MATT-ZANDSTRA/dp/1484219953/ref=sr_1_17?s=books&ie=UTF8&qid=1487268592&sr=1-17&keywords=php

A few to get you started.

no worries man, next time just show the last one that didnt work as it could be your closest to success,

as for the books to learn php here are a few of my favs,
PHP for Absolute Beginners
PHP Essentials
PHP and MySQL For Dummies

i cant really speak for everyone however I would never pick on you for your failures as i truly believe that without failure there would be no success.

That is actually exactly what I meant. Learning is a journey by itself, but all of these are outdated and show bad practices by todays standards. As a general rule, any book you look at that uses mysql_ functions should be left on the shelve. Things like this sql statement:

[php]$sql = "INSERT INTO Users (username, password) VALUES (’$username’, ‘$password’)[/php]

Will do nothing but lead you down the wrong path.

You may not think you are “getting it”, but you are actually learning what doesn’t work.

yea i guess i need to update my library … didn’t realize how old they were …

Don’t feel too bad, I have books dating back to PHP 4. But, I’ve been at this a while!

Isn’t that sort of like what Thomas Edison once said, “I have not failed. I have found 10,000 ways that won’t work.”

Isn't that sort of like what Thomas Edison once said, "I have not failed. I have found 10,000 ways that won't work."

LOL! That is seriously funny and very true.

This may be a stupid question, but here goes anyway. Is there a certain place where I should put this code to check for email address typo errors?

Here is the code I have so far for the form and if if need be I could post the form HTML code for the form itself as well.

[php]<?php

/* Subject and Email Variables */

$emailSubject = ‘2017 Windemuth Family Reunion Registration Form’;

/* Gathering Data Variables */

$Descendant = $_POST[‘descendant’];
$Spouse = $_POST[‘spouse’];
$Child1 = $_POST[‘child1’];
$Child2 = $_POST[‘child2’];
$Child3 = $_POST[‘child3’];
$Child4 = $_POST[‘child4’];
$Street = $_POST[‘street’];
$City = $_POST[‘city’];
$StateProvince = $_POST[‘stateprovince’];
$PostalCode = $_POST[‘postalcode’];
$Phone = $_POST[‘phone’];
$Email = $_POST[‘email’];
$Confirm = $_POST[‘confirm’];
$Adults = $_POST[‘adults’];
$Children = $_POST[‘children’];
$RegistrationFee = $_POST[‘registrationfee’];
$Comments = $_POST[‘comments’];

$webMaster = $Descendant.‘[email protected]’;

$immigrant="";
foreach($_POST[“immigrant”] as $option){
$immigrant .= $option;
}

$body = <<<EOD




Descendant: $Descendant

Spouse: $Spouse

Child1: $Child1

Child2: $Child2

Child3: $Child3

Child4: $Child4

Street: $Street

City: $City

StateProvince: $StateProvince

PostalCode: $PostalCode

Phone: $Phone

Email: $Email

Confirm: $Confirm

Descended From: $immigrant

Adults: $Adults

Children: $Children

RegistrationFee: $RegistrationFee

Comments: $Comments

EOD;

$headers = “From: $email\r\n”;
$headers .= “Content-type: text/html\r\n”;
$success = mail ($webMaster, $emailSubject, $body, $headers);

echo “Your Registration has been submitted successfully. We will contact you by email and confirm your Registration within 48 hours.”;

?>
[php][/php][/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service