Form going to 404 page when name input has a value

2019-06-02 09:02发布

I am completely stumped with this (that, or I've just been staring at it for too long that there is an obvious error and I can't see it)

I've got a simple form which validates on the same page which is a custom template I have made

<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>

It is then validated like so

<?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 } ?>

It shows the errors when you try to send an empty form, it validates the email fine when there is a value in there and when the name field is empty. When something is entered into the name field when there is something or nothing in the email field it goes to my 404 page.

3条回答
放荡不羁爱自由
2楼-- · 2019-06-02 09:21

You can do this --

<?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; 
}
?>
查看更多
唯我独甜
3楼-- · 2019-06-02 09:24

The solution is very simple, avoid using in forms the name attribute called "name".

change:

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

to:

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

Additional info:

Unsafe Names for HTML Form Controls

查看更多
疯言疯语
4楼-- · 2019-06-02 09:42

For me it works fine in this way, try replace your PHP with this:

<?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>';
}
?>  
查看更多
登录 后发表回答