PHP错误:所有字段都是必填字段

时间:2018-03-01 19:29:34

标签: php html forms

我是PHP的新手(在大学上学了一些课,但已经很长时间了)当我提交表单时我一直收到这个错误我不知道错误来自何处并且让我发疯!错误说:错误:所有字段都是必需的。填写完所有表单字段后会弹出。谢谢任何可以提供帮助的人!

这是我的HTML: {                 

            <section id="book-appointment" class="book-appointment gray" style="padding-top: 100px">
                <div class="grid-container grid-container-padded shadow radius">
                    <div class="grid-x">
                        <div class="cell small-12">
                            <header class="section-head">
                                <h3 class="section-title text-center ">
                                    Say Hello!
                                </h3><!-- /.section-title -->

                                <div class="section-head-actions text-center">
                                    Reach us for any questions you might have
                                </div><!-- /.section-head-actions -->
                            </header><!-- /.section-head -->
                        </div>
                        <div class="section-body">
                            <form data-abide novalidate>


                                <!-- First Name -->
                                <div class="grid-x grid-margin-x">
                                    <div class="cell small-12 medium-6">
                                        <label><p>First Name</p>
                                            <input type="text" name="firstname" placeholder="First Name" aria-describedby="exampleHelpText" required>

                                        </label>
                                    </div>

                                    <!-- Last Name -->
                                    <div class="cell small-12 medium-6">
                                        <label><p>Last Name</p>
                                            <input type="text" name="lastname" placeholder="Last Name" aria-describedby="exampleHelpText" required>

                                        </label>
                                    </div>

                                    <!-- Email -->
                                    <div class="cell small-12 medium-6">
                                        <label><p>Email</p>
                                            <input type="email" name="email" placeholder="Email" aria-describedby="exampleHelpText" required>

                                        </label>
                                    </div>


                                    <!-- Organization Name -->
                                    <div class="cell small-12 medium-6">
                                        <label><p>Organization Name</p>
                                            <input type="text" name="orgname" placeholder="Organization" aria-describedby="exampleHelpText" required>

                                        </label>
                                    </div>

                                        <!-- Board Memebers -->
                                    <div class="cell small-12 medium-6">
                                        <label><p>Board Members</p>
                                            <input type="text" name="board" placeholder="Board Memebers" aria-describedby="exampleHelpText" required>

                                        </label>
                                    </div>

                                    <!-- Info -->
                                    <div class="cell small-12 medium-6">
                                        <label><p>Organization Info? </p>
                                            <input type="text" name="info" placeholder="Organization Info" aria-describedby="exampleHelpText" required>

                                        </label>
                                    </div>


                                    <!-- Budget -->
                                    <div class="cell small-12 medium-6">
                                        <label><p>Budget</p>
                                            <input type="text" name="budget" placeholder="Budget" aria-describedby="exampleHelpText" required>

                                        </label>
                                    </div>



                                </div><!-- /.grid-x .grid-margin-x -->

                                <!-- Textarea -->
                                <div class="grid-x grid-margin-x">
                                    <div class="cell">
                                        <label><p>Project Info</p>
                                            <textarea class="textarea" name="field_message" rows="8" placeholder="Project Info" required></textarea>
                                        </label>
                                    </div>
                                </div><!-- /.grid-x -->

                                <!-- Submit Button -->
                                <div class="grid-x">
                                    <fieldset class="cell auto text-center">
                                        <button class="button action-button gradient-f-100" type="submit" value="Submit">Send <i class="fa fa-paper-plane" aria-hidden="true"></i></button>
                                    </fieldset>
                                </div>
                            </form>
                        </div>
                    </div>      
                </div>
            </section><!-- /.book-appointment -->

}

这是我的PHP:

<?php 
$errors = '';
$myemail = 'email@hotmail.com';//
if(empty($_GET['firstname'])  || 
empty($_GET['email']) || 
empty($_GET['field_message']))
{
$errors .= "\n Error: all fields are required";
}

$firstname = $_GET['firstname']; 
$lastname= $_GET ['lastname'];
$email = $_GET['email']; 
$orgname = $_GET['orgname'];
$board = $_GET['board'];
$info = $_GET ['info'];
$budget = $_GET ['budget'];
$field_message = $_GET ['field_message'];


if (!preg_match(
"/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i", 
$email))
{
$errors .= "\n Error: Invalid email address";
}

if( empty($errors))
{
$to = $myemail; 
$email_subject = "Form submission: $firstname $lastname";
$email_body = "You have received a new message. ".
" Here are the details:\n Name: $firstname \n Last Name: $lastname \n Email: 
$email \n Organization Name: $orgname \n Board: $board \n Organization Info: 
$info \n Budget: $budget \n Message \n $field_message"; 

$headers = "From: $myemail\n"; 
$headers .= "Reply-To: $email";

mail($to,$email_subject,$email_body,$headers);
//redirect to the 'thank you' page
include ('Location: contact-form-thank-you.html');
} 
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
"http://www.w3.org/TR/html4/loose.dtd"> 
<html>
<head>
<title>Contact form handler</title>
</head>

<body>
<!-- This page is displayed only if there is some error -->
<?php
echo nl2br($errors);
?>


</body>
</html>

2 个答案:

答案 0 :(得分:2)

此处有一些名称不正确的变量(应为$_GET,因为您的表单默认使用该方法,并且没有名为&#39; name&#39;或&#39; message&#39;)的表单字段,所以你的错误来自这段代码:

if(empty($_POST['name'])  || 
    empty($_POST['email']) || 
    empty($_POST['message']))
{
$errors .= "\n Error: all fields are required";
}

我强烈怀疑你想要以下字段:

if(empty($_GET['firstname'])  || 
    empty($_GET['email']) || 
    empty($_GET['field_message']))
{
$errors .= "\n Error: all fields are required";
}

您的表单没有指定方法,因此默认为GET。这意味着您的所有变量都位于$_GET数组中,而不是$_POST

$firstname = $_GET['firstname']; 
$lastname= $_GET ['lastname'];
$email = $_GET['email']; 
$orgname = $_GET['orgname'];
$board = $_GET['board'];
$info = $_GET ['info'];
$budget = $_GET ['budget'];
$field_message = $_GET ['field_message'];

您还可以简化以下代码块,而不是使用复杂且可能不准确的正则表达式:

if (!filter_var($email, FILTER_VALIDATE_EMAIL))
{
$errors .= "\n Error: Invalid email address";
}

答案 1 :(得分:-1)

这不是您在发布数据时遇到的php问题,因此您的表单需要在帖子中提交数据。

<form method="post" data-abide novalidate>