I am new to PHP (took some classes in college but its been a long time) and I keep getting this error when I submit my form Im not sure where the error is coming from and its driving me crazy! The error says: Error: all fields are required. This pops up when all the form fields have been filled out. Thank you to anyone that can help!
Here is my 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 -->
}
And here is my 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>
You have some misnamed variables here (should be $_GET
because your form defaults to that method and there are no form fields named 'name' or 'message'), so your error is coming from this block of code:
if(empty($_POST['name']) ||
empty($_POST['email']) ||
empty($_POST['message']))
{
$errors .= "\n Error: all fields are required";
}
I strongly suspect you want the following fields:
if(empty($_GET['firstname']) ||
empty($_GET['email']) ||
empty($_GET['field_message']))
{
$errors .= "\n Error: all fields are required";
}
Your form has no method specified, so it defaults to GET. This means all of your variables will be in the $_GET
array, not $_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'];
You can also simplify the following block of code, instead of using complex and possibly inaccurate regex:
if (!filter_var($email, FILTER_VALIDATE_EMAIL))
{
$errors .= "\n Error: Invalid email address";
}
这不是您在帖子数据中查看的 php 问题,因此您的表单需要在帖子中提交数据。
<form method="post" data-abide novalidate>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.