将 phone 字段添加到联系表单
Adding phone field to contact form
我对 php 几乎一无所知。所以我试图通过添加 phone 字段来编辑我网站上的联系表格。问题是,一旦我这样做,它就会停止工作,而且我的收件箱中也收不到消息。我可能做错了什么,但我不知道它是什么。
这是联系表格的完整代码:
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<?php
if(isset($_POST['email'])) {
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "myemail@gmail.com";
$email_subject = "Contact Form AA";
function died($error) {
// your error code can go here
echo "We are sorry, an error has occurred.";
echo "The error is the following: <br /><br />";
echo $error."<br /><br />";
echo "Please fix the error.<br /><br />";
die();
}
// validation expected data exists
if(!isset($_POST['first_name']) ||
!isset($_POST['email']) ||
!isset($_POST['phone']) ||
!isset($_POST['comments'])) {
died('We are sorry, an error has occurred.');
}
$first_name = $_POST['first_name']; // required
$email_from = $_POST['email']; // required
$phone = $_POST['phone']; // required
$comments = $_POST['comments']; // required
$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if(!preg_match($email_exp,$email_from)) {
$error_message .= 'The email is not a valid one.<br />';
}
if(strlen($first_name) < 2) {
$error_message .= 'The name is not a valid one.<br />';
}
if(strlen($comments) < 2) {
$error_message .= 'The message is not a valid one.<br />';
}
if(strlen($error_message) > 0) {
died($error_message);
}
$email_message = "Contact details:\n\n";
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$email_message .= "Name: ".clean_string($first_name)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "Phone: ".clean_string($phone)."\n";
$email_message .= "Menssage: ".clean_string($comments)."\n";
// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $phone, $headers);
header("Location: thankyou.html");
?>
<?php
}
?>
这是我刚刚添加的行:
!isset($_POST['phone']) ||
$phone = $_POST['phone']; // required
$email_message .= "Phone: ".clean_string($phone)."\n";
还有单词$phone
,最后
谁能看出问题出在哪里?这是我使用的简单形式。和以前一样,我只添加了 phone 部分。
<form name="contactform" method="post" action="send_form_email.php">
<div class="controls">
<input type="text" name="first_name" placeholder="Name" id="name" required data-validation-required-message="Please enter your name" />
</div>
<div class="controls">
<input type="email" placeholder="Email" name="email" id="email" required data-validation-required-message="Please enter your email address" />
</div>
<div class="controls">
<input type="tel" placeholder="Phone" name="phone" id="phone" required data-validation-required-message="Please enter your phone number" />
</div>
<div class="controls">
<textarea name="comments" rows="5" cols="100" placeholder="Message" id="message" required data-validation-required-message="Please enter your message" maxlength="999" style="resize:none"></textarea>
</div>
<button type="submit" class="btn-primary submit">SEND!<i class="fa fa-paper-plane sub"></i></button>
</form>
感谢您的帮助!
<input type="phone" placeholder="Phone" name="phone" id="phone" required data-validation-required-message="Please enter your phone number" />
</div>
将此行更改为
<input type="text" placeholder="Phone" name="phone" id="phone" required data-validation-required-message="Please enter your phone number" />
</div>
就像比拉尔说的
<input type="phone" placeholder="Phone" name="phone" id="phone" required data-validation-required-message="Please enter your phone number" />
</div>
不正确,但是您需要使用类型 tel
<input type="tel" placeholder="Phone" name="phone" id="phone" required data-validation-required-message="Please enter your phone number" />
</div>
编辑:
你的邮件语法也不对
从邮件函数中删除 $phone
,因为那只是错误的语法 + 您已经将 $phone 放入消息
所以你的语法应该是这样的:
mail($email_to, $email_subject, $email_message, $headers);
您可以将 phone 字段更改为 type=number 或 type=text。
<input type="text" placeholder="Phone" name="phone" id="phone" required data-validation-required-message="Please enter your phone number" />
注意:Internet Explorer 9 及更早版本不支持 type="number" 的元素。
而且我认为您在邮件功能中不需要 $phone。
@mail($email_to, $email_subject, $email_message, $phone, $headers);
将此更改为。
@mail($email_to, $email_subject, $email_message, $headers);
这有望解决问题。
我对 php 几乎一无所知。所以我试图通过添加 phone 字段来编辑我网站上的联系表格。问题是,一旦我这样做,它就会停止工作,而且我的收件箱中也收不到消息。我可能做错了什么,但我不知道它是什么。
这是联系表格的完整代码:
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<?php
if(isset($_POST['email'])) {
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "myemail@gmail.com";
$email_subject = "Contact Form AA";
function died($error) {
// your error code can go here
echo "We are sorry, an error has occurred.";
echo "The error is the following: <br /><br />";
echo $error."<br /><br />";
echo "Please fix the error.<br /><br />";
die();
}
// validation expected data exists
if(!isset($_POST['first_name']) ||
!isset($_POST['email']) ||
!isset($_POST['phone']) ||
!isset($_POST['comments'])) {
died('We are sorry, an error has occurred.');
}
$first_name = $_POST['first_name']; // required
$email_from = $_POST['email']; // required
$phone = $_POST['phone']; // required
$comments = $_POST['comments']; // required
$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if(!preg_match($email_exp,$email_from)) {
$error_message .= 'The email is not a valid one.<br />';
}
if(strlen($first_name) < 2) {
$error_message .= 'The name is not a valid one.<br />';
}
if(strlen($comments) < 2) {
$error_message .= 'The message is not a valid one.<br />';
}
if(strlen($error_message) > 0) {
died($error_message);
}
$email_message = "Contact details:\n\n";
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$email_message .= "Name: ".clean_string($first_name)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "Phone: ".clean_string($phone)."\n";
$email_message .= "Menssage: ".clean_string($comments)."\n";
// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $phone, $headers);
header("Location: thankyou.html");
?>
<?php
}
?>
这是我刚刚添加的行:
!isset($_POST['phone']) ||
$phone = $_POST['phone']; // required
$email_message .= "Phone: ".clean_string($phone)."\n";
还有单词$phone
,最后
谁能看出问题出在哪里?这是我使用的简单形式。和以前一样,我只添加了 phone 部分。
<form name="contactform" method="post" action="send_form_email.php">
<div class="controls">
<input type="text" name="first_name" placeholder="Name" id="name" required data-validation-required-message="Please enter your name" />
</div>
<div class="controls">
<input type="email" placeholder="Email" name="email" id="email" required data-validation-required-message="Please enter your email address" />
</div>
<div class="controls">
<input type="tel" placeholder="Phone" name="phone" id="phone" required data-validation-required-message="Please enter your phone number" />
</div>
<div class="controls">
<textarea name="comments" rows="5" cols="100" placeholder="Message" id="message" required data-validation-required-message="Please enter your message" maxlength="999" style="resize:none"></textarea>
</div>
<button type="submit" class="btn-primary submit">SEND!<i class="fa fa-paper-plane sub"></i></button>
</form>
感谢您的帮助!
<input type="phone" placeholder="Phone" name="phone" id="phone" required data-validation-required-message="Please enter your phone number" />
</div>
将此行更改为
<input type="text" placeholder="Phone" name="phone" id="phone" required data-validation-required-message="Please enter your phone number" />
</div>
就像比拉尔说的
<input type="phone" placeholder="Phone" name="phone" id="phone" required data-validation-required-message="Please enter your phone number" />
</div>
不正确,但是您需要使用类型 tel
<input type="tel" placeholder="Phone" name="phone" id="phone" required data-validation-required-message="Please enter your phone number" />
</div>
编辑:
你的邮件语法也不对
从邮件函数中删除 $phone
,因为那只是错误的语法 + 您已经将 $phone 放入消息
所以你的语法应该是这样的:
mail($email_to, $email_subject, $email_message, $headers);
您可以将 phone 字段更改为 type=number 或 type=text。
<input type="text" placeholder="Phone" name="phone" id="phone" required data-validation-required-message="Please enter your phone number" />
注意:Internet Explorer 9 及更早版本不支持 type="number" 的元素。
而且我认为您在邮件功能中不需要 $phone。
@mail($email_to, $email_subject, $email_message, $phone, $headers);
将此更改为。
@mail($email_to, $email_subject, $email_message, $headers);
这有望解决问题。