这是脚本和表单。当我测试它时,它会在有人甚至可以完成表单之前立即验证。我确定这是一件容易的事,因为这个脚本已经在我已经完成的其他网站上运行了。任何想法?
<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)
?>
答案 0 :(得分:1)
尝试更改代码中的此行 if(isset($ _ POST ['submitted'])){ .....................}到 if(isset) ($ _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)
?>
答案 1 :(得分:0)
似乎代码中可能存在混淆。
所以你想在表格完成后验证吗?将您的第一个if语句更改为
if (isset($_POST['completed'])) {
尝试删除它,因为它将始终在脚本运行时设置值,这将导致它每次都运行。保留null将阻止它运行,直到设置变量。
$completed = false;
将此添加到您的表单部分。它将设置一个值到完成,将运行验证脚本。
<input type="hidden" name="completed" value="yes" />
这些操作应该导致验证脚本在表单完成时运行。它可能不会巩固您的验证脚本,因为可能还需要简化其他一些事情,但这应该可以解决您当前遇到的问题。
答案 2 :(得分:0)
PHP不能处理.html文件。
更改操作=&#34; index.html&#34; to action =&#34; index.php&#34; 将index.html文件名更改为&#34; index.php&#34;
应该工作。
答案 3 :(得分:-1)
这比你拥有的更容易阅读。
<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)
如果在回声中使用单引号,则不需要从回声中执行'.escaped'。