Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
978 views
in Technique[技术] by (71.8m points)

checking if email and confirm email fields are the same using php

I would like to confirm that the email field and confirm email field match each other in my html form before submitting the form to the database, i have done the following but it doesn't seem to work:

<?php

if (!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
$_SESSION['error'][] = "Invalid email address";
}
    
if ($_POST['email'] != $_POST['confirmemail']) {
$_SESSION['error'][] = "Email addresses do not match";
}
          
else { 

this is followed by the script to submit the form to the database which works fine. Any suggestions would be welcome, many thanks

question from:https://stackoverflow.com/questions/65883391/checking-if-email-and-confirm-email-fields-are-the-same-using-php

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

There might be whitespace issue causing your comparison to fail. try trimming your inputs before comparison.

$email = trim( $_POST['email'] );
$confirmEmail = trim( $_POST['confirmemail'] );

if ($emsil != $confirmEmail ) {
   $_SESSION['error'][] = "Email addresses do not match";
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...