即使一切正确,它仍然说字段是空的。继承我的代码:
<p>*All Fields Required</p>
<?php
if(!empty($reply)){
echo "<p class='notify'>$reply</p>";
}
unset($reply);
?>
<form method="post" action="index.php" id='contact'>
<fieldset>
<label>Name</label>
<input type='text' id="name" name="name" placeholder="type here" required value='<?php echo $name; ?>'>
<label>Email</label>
<input type='email' id="email" name="email" placeholder="type here" required value='<?php echo $email; ?>'>
<label>Message</label>
<textarea id="message" name="message" placeholder="type here" required><?php echo $message; ?> </textarea>
<p>Answer the following CAPTCHA question: </p>
<label for="captcha">What color is Snow Whites hair?</label>
<input type='text' name='captcha' id='captcha' size='5'required><br>
<label for='action'> </label>
<input id="submit" name="submit" type="submit" value="submit"><br>
</fieldset>
</form>
继承我的index.php文件:
<?php
if($_POST['action']=='submit'){
$name=$_POST['name'];
$email=$_POST['email'];
$message=$_POST['message'];
$captcha=strtolower($_POST['captcha']);
}
if(empty($name)||empty($email)||empty($message)){
$reply='Sorry, one or more fields are empty. All fields are required.';
include 'contactform.php';
exit;
}
if(empty($captcha)|| $captcha != 'black'){
$reply='The captcha answer is incorrect.';
include 'contactform.php';
exit;
}
$finalmessage="Name:$name\n";
$finalmessage .="Email: $email\n";
$finalmessage .="Message: \n$message";
//sending the message
$to="rachel14yancey@gmail.com";
$from="From: $email";
$result= mail($to, $finalmessage, $from);
//Letting visitors know what happened
if($result ==TRUE){
$reply = "Thank you $name for contacting us.";
unset($name);
unset($email);
unset($message);
include 'contactform.php';
exit;
} else{
$reply='Sorry $name. There was an error and the message could not be sent';
unset($name);
unset($email);
unset($message);
include 'contactform.php';
exit;
}
?>
答案 0 :(得分:2)
您没有名为action
的表单字段,因此
if($_POST['action']=='submit'){
总是评估为false,将您尝试创建的所有变量都保留为未定义。
您可能需要$_POST['submit']
,因为这是您实际的submit
按钮的名称。
<input id="submit" name="submit" type="submit" value="submit"><br>
^^^^^^^^^^^^^^^^^^^^^^^^^^^
答案 1 :(得分:1)
这是表格提交的内容:
*var_dump($_POST)
**array (size=5)
'name' => string 'John Doe' (length=6)
'email' => string 'John.Doe@mail' (length=13)
'message' => string 'Blah Blah Blah' (length=10)
'captcha' => string 'White' (length=5)
'submit' => string 'submit' (length=6)**
而不是$ _POST [“action”]你可以使用$ _POST [“submit”]它应该可以工作。