我想在联系表单中添加其他文本字段“电话号码”。我正在使用的Bootstrap模板中已经存在联系表,并且无需添加即可正常运行。我可能写错了PHP。
在不对PHP进行任何更改的情况下,联系表单将发送一封没有问题的电子邮件。
HTML
rootDirs
PHP
<form>
<div>
<div class="col-md-12">
<div class="form-group">
<input class="form-control" type="email" name="email" placeholder="E-mail *" required="">
<p class="help-block text-danger"></p>
</div>
</div>
<div class="col-md-12">
<div class="form-group">
<input class="form-control" type="text" name="number" placeholder="Telephone Number *" required="">
<p class="help-block text-danger"></p>
</div>
</div>
<div class="col-md-12">
<div class="form-group">
<input class="form-control" type="text" name="subject" placeholder="Subject *" required="">
<p class="help-block text-danger"></p>
</div>
</div>
<div class="col-md-12">
<div class="form-group">
<textarea class="form-control" name="message" placeholder="Message *" rows="12" required=""></textarea>
<p class="help-block text-danger"></p>
</div>
</div>
<div class="col-md-12">
<p>
<input class="btn btn-block btn-brand" type="submit" value="Send Message">
</p>
</div>
</div>
</form>
<!-- Ajax response-->
<div class="ajax-response text-center" id="contact-response"></div>
JAVASCRIPT
// Mail settings
$to = "example@gmail.com";
// You can put here your email
$header = "From: example@gmail.com\r\n";
$header.= "MIME-Version: 1.0\r\n";
$header.= "Content-Type: text/plain; charset=utf-8\r\n";
$header.= "X-Priority: 1\r\n";
if (isset($_POST["name"]) && isset($_POST["email"]) && isset($_POST["number"]) && isset($_POST["subject"]) && isset($_POST["message"])) {
$content = "Name: " . $_POST["name"] . "\r\n";
$content .= "Email: " . $_POST["email"] . "\r\n";
$content .= "Number: " . $_POST["number"] . "\r\n";
$content .= "Subject: " . $_POST["subject"] . "\r\n";
$content .= "Message: " . "\r\n" . $_POST["message"];
$subject = $_POST["subject"];
if (mail($to, $subject, $content, $header)) {
$result = array(
"message" => "Thanks for contacting us.",
"sendstatus" => 1
);
echo json_encode($result);
} else {
$result = array(
"message" => "Sorry, something is wrong.",
"sendstatus" => 0
);
echo json_encode($result);
}
}
联系表中的电子邮件应成功发送。
答案 0 :(得分:0)
在您的HTML和PHP代码中,您的电话号码字段称为number
,但您的javascript却在查找名为“电话”的字段,如以下代码行所示:
var phone = $('#contact-form [name="phone"]').val();
我建议您在HTML和PHP上进行更改,以便该字段如javascript所期望的那样被命名为“ phone”
正在发生的事情是,该javascript代码仅采用代码中列出的特定字段,然后将其传递给PHP,但由于它与名称不匹配,因此该字段被遗忘了,因此对PHP而言似乎不存在脚本,并且为空,验证失败