我完全被这个难住了(那个,或者我只是盯着它太久了,有一个明显的错误,我看不到它)
我有一个简单的表单,可以在同一页面上验证,这是我制作的自定义模板
<form action="" method="post">
<input type="text" id="name" name="name" placeholder="Please enter your full name" />
<input type="email" id="email" name="email" placeholder="Please enter your email address" />
<input type="hidden" name="newsletterform" value="newsletterform" />
<input class="submit" type="submit" name="submit" id="searchsubmit" value="submit" />
</form>
然后像这样验证
<?php
$formsubmitted = (!empty($_POST['newsletterform'])) ? $_POST['newsletterform'] : "";
if($formsubmitted !== ''){
$fullname = (!empty($_POST['name'])) ? $_POST['name'] : "";
$email = (!empty($_POST['email'])) ? $_POST['email'] : "";
$failed = '';
if (empty($fullname)){
$failed.= 'Please enter your name.<br />';
}
if (empty($email)){
$failed.= 'Please enter your email address.<br />';
}
}
if ($failed == '' and $formsubmitted !== ''){ ?>
<div id="success-title">
Thank you for signing up!
</div>
<?php
}
if ($failed !== '' and $formsubmitted !== '') { ?>
<div id="error-title">
Sorry, we could not accept the details you submitted to us. Please see below.
</div>
<div id="error-body">
<?php echo $failed;?>
</div>
<?php } ?>
当您尝试发送空表单时,它会显示错误,当其中有值并且名称字段为空时,它会验证电子邮件是否正常。当在名称字段中输入某些内容时,当电子邮件字段中有内容或没有内容时,它会转到我的 404 页面。