[英]php script for contact form
這是腳本和形式。 當我對其進行測試時,它會立即驗證,甚至沒有人可以填寫表格。 我敢肯定這是一件容易的事,因為此腳本已在我完成的其他站點上工作。 有任何想法嗎?
<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)
?>
嘗試從以下位置更改代碼中的這一行
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)
?>
似乎代碼中有些混淆。
因此,您想在表格填寫后進行驗證嗎? 將您的第一個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)
如果在回顯內使用單引號,則無需執行“ .escapeed from echo”。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.