1

我完全被这个难住了(那个,或者我只是盯着它太久了,有一个明显的错误,我看不到它)

我有一个简单的表单,可以在同一页面上验证,这是我制作的自定义模板

<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 页面。

4

3 回答 3

3

解决方法很简单,避免在表单中使用name名为“name”的属性。

改变:

<input type="text" id="name" name="name" placeholder="Please enter your full name" />

至:

<input type="text" id="name" name="myname" placeholder="Please enter your full name" />

附加信息:

HTML 表单控件的不安全名称

于 2013-06-05T14:02:52.213 回答
0

你可以这样做 -

<?php
if (strlen($fullname) < 3) {
            // name too short, add error
            $errors['name_error'] = 'Your name is required';
        }elseif(strlen($fullname) ==0){
         $errors['name_error'] = 'Your name is required';
        }

        if (strlen($email) == 0) {
            // no email address given
            $errors['email_error'] = 'Email address is required';
        } else if ( !preg_match('/^(?:[\w\d]+\.?)+@(?:(?:[\w\d]\-?)+\.)+\w{2,4}$/i', $email)) {
            // invalid email format
            $errors['email_error'] = 'Email address entered is invalid';
        }

    if (sizeof($errors) == 0) {
 echo '<div id="success-title">
    Thank you for signing up!
</div>';
     }
     else {
     echo 'Sorry, we could not accept the details you submitted to us. Please see below.' . $errors; 
}
?>
于 2013-06-05T13:40:44.713 回答
0

对我来说它以这种方式工作正常,尝试用这个替换你的PHP:

<?php
$failed = '';

  $formsubmitted = (!empty($_POST['newsletterform'])) ? $_POST['newsletterform'] : "";
   if($formsubmitted !== ''){
    $fullname = (!empty($_POST['name'])) ? $_POST['name'] : "";
    $email = (!empty($_POST['email'])) ? $_POST['email'] : "";

    if (empty($fullname)){
        $failed.= 'Please enter your name.<br />';
    }
    if (empty($email)){
        $failed.= 'Please enter your email address.<br />';
    }
}
if ($failed == '' and $formsubmitted !== ''){ 
    echo '<div id="success-title">
        Thank you for signing up!
    </div>';

}
if ($failed !== '' and $formsubmitted !== '') { 

    echo '<div id="error-title">
        Sorry, we could not accept the details you submitted to us. Please see below.
    </div>
    <div id="error-body">';
    echo $failed;
    echo '</div>';
}
?>  
于 2013-06-05T13:23:27.303 回答