这是脚本和表单。当我对它进行测试时,它甚至在有人完成表单之前就立即验证了。我相信这是一件很容易的事情,因为这个脚本已经在我做过的其他网站上工作了。什么好主意吗?
<form id="contact" form method="post" action="index.html">
<div class="row half">
<?php //form validation and email sending will go here
//check to see if the form has been completely filled out or not
$completed = false;
if (isset($_POST['submitted'])) {
if (!empty($_POST['name'])) {
$name = $_POST['name'];
} else {
$name = NULL;
echo '<p class="error">You forgot to enter your name!</p>';
}
if (preg_match ('/^['w.-]+@['w.-]+'.[A-Za-z]{2,6}$/', $_POST['email'])) {
$email = $_POST['email'];
} else {
$email = NULL;
echo '<p class="error">Please enter a valid email address.</p>';
}
if (!empty($_POST['message'])) {
$comments = $_POST['message'];
} else {
$comments = NULL;
echo '<p class="error">Please include your comments.</p>';
}
//form has been completed, send email
if ($name && $email && $comments) {
//form has been completed, set to true
$completed = true;
$to = "companion@hotmail.com";
$subject = "Contact Form Submission";
$message = "Thank you for visiting The Companion!";
"Name: " . $name . "'r'n".
"Email: " . $email . "'r'n".
"Comments: " . $comments . "'r'n";
$headers = "From: companion@hotmail.com 'r'n";
$headers .= "Cc: ". $email. "'r'n";
mail($to,$subject,$message,$headers);
echo '<p class="confirmation">Thank you for contacting us.</br>
We received your message and will get back to you shortly.</p>';
} else {
//displays if the user has not completely filled out form
echo '<p class="error">The form has not been filled out completely, please check and try again.</p>';
} //end if ($name && $email && $comments)
}//end (isset($_POST['submitted'])
?>
<div class="6u">
<input type="text" name="name" placeholder="Name" value="<?php if(isset($_POST['name'])) echo $_POST['name']; ?>"/></div>
<div class="6u">
<input type="text" name="email" placeholder="Email" value="<?php if(isset($_POST['email'])) echo $_POST['email']; ?>"/></div>
</div>
<div class="row half">
<div class="12u">
<textarea name="message" placeholder="Message" rows="6"><?php if (isset($_POST['comments'])) echo $_POST['comments']; ?></textarea></div>
</div>
<div class="row">
<div class="12u"><ul class="actions">
<li><input type="submit" name="submit" class="button" value="Send Message" /></li>
</ul>
</div>
</div>
</form>
<?php
} //end if ($completed == false)
?>
尝试从
更改代码中的这一行如果(收取($ _POST['提交'])){.....................} 如果(收取($ _POST['提交'])){..............}。
这是因为您在表单submit中使用了name="submit"。并尝试使用$_POST['submitted']检查循环。
所以你的代码现在看起来像…
index . php。
<form id="contact" method="post" action="index.php">
<div class="row half">
<?php //form validation and email sending will go here
//check to see if the form has been completely filled out or not
$completed = false;
if (isset($_POST['submit'])) {
if (!empty($_POST['name'])) {
$name = $_POST['name'];
} else {
$name = NULL;
echo '<p class="error">You forgot to enter your name!</p>';
}
if (preg_match ('/^['w.-]+@['w.-]+'.[A-Za-z]{2,6}$/', $_POST['email'])) {
$email = $_POST['email'];
} else {
$email = NULL;
echo '<p class="error">Please enter a valid email address.</p>';
}
if (!empty($_POST['message'])) {
$comments = $_POST['message'];
} else {
$comments = NULL;
echo '<p class="error">Please include your comments.</p>';
}
//form has been completed, send email
if ($name && $email && $comments) {
//form has been completed, set to true
$completed = true;
$to = "champanion@hotmail.com";
$subject = "Contact Form Submission";
$message = "Thank you for visiting The Champanion!";
"Name: " . $name . "'r'n".
"Email: " . $email . "'r'n".
"Comments: " . $comments . "'r'n";
$headers = "From: champanion@hotmail.com 'r'n";
$headers .= "Cc: ". $email. "'r'n";
mail($to,$subject,$message,$headers);
echo '<p class="confirmation">Thank you for contacting us.</br>
We received your message and will get back to you shortly.</p>';
} else {
//displays if the user has not completely filled out form
echo '<p class="error">The form has not been filled out completely, please check and try again.</p>';
} //end if ($name && $email && $comments)
}//end (isset($_POST['submit'])
?>
<div class="6u">
<input type="text" name="name" placeholder="Name" value="<?php if(isset($_POST['name'])) echo $_POST['name']; ?>"/></div>
<div class="6u">
<input type="text" name="email" placeholder="Email" value="<?php if(isset($_POST['email'])) echo $_POST['email']; ?>"/></div>
</div>
<div class="row half">
<div class="12u">
<textarea name="message" placeholder="Message" rows="6"><?php if (isset($_POST['comments'])) echo $_POST['comments']; ?></textarea></div>
</div>
<div class="row">
<div class="12u"><ul class="actions">
<li><input type="submit" name="submit" class="button" value="Send Message" /></li>
</ul>
</div>
</div>
</form>
<?php
//end if ($completed == false)
?>
看起来可能在代码中出现了混淆。
所以您想在表单完成后验证?将第一个if语句更改为
if (isset($_POST['completed'])) {
尝试删除这个,因为它总是在脚本运行时设置该值,这将导致它每次都运行。将其设置为空将阻止它运行,直到设置好变量。
$completed = false;
将其添加到表单部分。它将设置一个值,并运行验证脚本。
<input type="hidden" name="completed" value="yes" />
当表单完成时,这些操作将导致验证脚本运行。它可能不会固化您的验证脚本,因为可能还有其他一些东西需要简化,但这应该可以解决您当前遇到的问题。
PHP不能在。html文件上工作。
将action="index.html"更改为action="index.php"将index.html文件名改为"index.php"
工作。
这比你看到的更容易阅读。
<div class="6u">
<input type="text" name="name" placeholder="Name" value="'.$name.'/></div>
<div class="6u">
<input type="text" name="email" placeholder="Email" value="'.$mail.'"/></div>
</div>
<div class="row half">
<div class="12u">
<textarea name="message" placeholder="Message" rows="6">'.$comments.'</textarea></div>
</div>
<div class="row">
<div class="12u"><ul class="actions">
<li><input type="submit" name="submit" class="button" value="Send Message" /></li>
</ul>
</div>
</div>
</form>';
} //end if ($completed == false)
如果你在echo中使用单引号,你就不需要做'.escaped from echo.'