-2

我有以下代码,我想验证表单以确保在提交 for 之前两封电子邮件都匹配。

电子邮件在第 24 和 26 行,提前致谢。

<?php
include_once 'includes/config.php';
$pageTitle = 'Members';
include HEADER;
?>

<?php
$fname  = "";
$lname  = "";
$email1 = "";
$email2 = "";
?>

<p>To create an account with us please fill out the form below</p>

<form action="sendRegister.php" method="get">
    First Name: <input name="new[fname]" type="text"><br>
    Last Name: <input name="new[lname]" type="text"><br>
</form>

Email : <input name="new[email1]" required="" type="email" value="<?php echo $email1;?>"><br>
Re-Enter Email : <input name="new[email2]" required="" type="email" value="<?php echo $email2;?>"><br>
Password: <input name="new[password1]" type="password"><br>
Re-enter password:<input name="new[password2]" type="password"><br>
Gender: <select name="new[gender]">
    <option value="Male">
        Male
    </option>

    <option value="Female">
        Female
    </option>
</select><br>
State of Residence:<select name="new[states]">
    <option value="NY">
        New York
    </option>

    <option value="NJ">
        New Jersey
    </option>
</select><br>
<br>
<?php 
if(isset($email1) != $email2){
echo 'please correct your email';
}
?>
<input type='submit' value='submit'>
</form>
<?php 
include FOOTER;
?>
4

3 回答 3

0

如果您想在提交表单之前进行检查,则必须使用 javascript 进行。

第二种可能性是:将您提交的表单值作为默认值,例如:

<?php
$pass1=$_POST["pass1"];
$pass2=$_POST["pass2"];
if (isset($pass1,$pass2) && $pass1!=$pass2) echo 'Your password confirmation is not equal to your password';
echo '<form ....>';
echo '<input type="text" name="pass1" value="'.$pass1.'">';
echo '<input type="text" name="pass2" value="'.$pass2.'">';
echo '</form>';
?>
于 2013-02-23T22:18:46.573 回答
0

尝试:

if(isset($email1, $email2)) {
  if($email1 != $email2){
    echo "Bad Email"
    ....
  }
}

您也可以使用 javascript 来执行此操作 ( http://www.w3schools.com/js/js_form_validation.asp )

于 2013-02-23T21:39:07.317 回答
0

我认为最好为此使用 JavaScript,特别是如果您希望在提交之前对其进行验证,因为(afaik)PHP 必须在提交时进行检查,而您可以使用 JavaScript 在单击提交按钮时进行检查,具体取决于验证的结果进行。

看看:W3Schools JS 表单验证

于 2013-02-23T21:39:51.663 回答