我正在尝试为我的网站实施一个基本联系表单,其中包含三个字段:姓名,电子邮件和消息。我正在使用的PHP代码如下:
<?php
// Fetching Values from URL.
$name = $_POST['name2'];
$email = $_POST['email2'];
$message = $_POST['message2'];
$email = filter_var($email, FILTER_SANITIZE_EMAIL); // Sanitizing E-mail.
// After sanitization Validation is performed
if (filter_var($email, FILTER_VALIDATE_EMAIL))
{
$subject = $name;
// To send HTML mail, the Content-type header must be set.
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From:' . $email. "\r\n"; // Sender's Email
$headers .= 'Cc:' . $email. "\r\n"; // Carbon copy to Sender
$template = '<div style="padding:50px; color:white;">Hello ' . $name . ',<br/>'
. '<br/>Thank you for contacting me!<br/><br/>'
. 'Name:' . $name . '<br/>'
. 'Email:' . $email . '<br/>'
. 'Message:' . $message . '<br/><br/>'
. 'This is a Contact Confirmation mail.'
. '<br/>'
. 'I\'ll get back to you as soon as possible .</div>';
$sendmessage = "<div style=\"background-color:#7E7E7E; color:white;\">" . $template . "</div>";
// Message lines should not exceed 70 characters (PHP rule), so wrap it.
$sendmessage = wordwrap($sendmessage, 70);
// Send mail by PHP Mail Function.
mail("myemailid@gmail.com", $subject, $sendmessage, $headers);
echo "Your message has been sent successfully!";
}
else
{
echo "<span>* invalid email *</span>";
}
?>
(显然我用实际的电子邮件地址替换了myemailid@gmail.com)
调用此php代码的JavaScript文件如下:
$(document).ready(function() {
$("#submit").click(function() {
var name = $("#name2").val();
var email = $("#email2").val();
var message = $("#message2").val();
// Checking for blank fields.
if (name == '' || email == '' || message == '')
{
alert("Please Fill Required Fields");
}
else
{
$.post("contact_form.php",
{
name2: name,
email2: email,
message2: message,
}, function(data) {
if (data == "Your message has been sent successfully!") {
$("#footer-form")[0].reset(); // To reset form fields on success.
}
});
}
});
});
点击发送&#39;按钮没有任何反应我在这做错了什么?我知道这是基本的,但一些帮助将非常感激!
如果这有帮助:
<div class="col-sm-6">
<div class="footer-content">
<form role="form" id="footer-form">
<div class="form-group has-feedback">
<label class="sr-only" for="name2">Name</label>
<input type="text" class="form-control" id="name2" placeholder="Name" name="name2" required>
<i class="fa fa-user form-control-feedback"></i>
</div>
<div class="form-group has-feedback">
<label class="sr-only" for="email2">Email address</label>
<input type="email" class="form-control" id="email2" placeholder="Enter email" name="email2" required>
<i class="fa fa-envelope form-control-feedback"></i>
</div>
<div class="form-group has-feedback">
<label class="sr-only" for="message2">Message</label>
<textarea class="form-control" rows="8" id="message2" placeholder="Message" name="message2" required></textarea>
<i class="fa fa-pencil form-control-feedback"></i>
</div>
<input type="button" id="submit" value="Send" class="btn btn-default">
</form>
</div>
</div>
此外,我点击发送按钮后的php URL如下:
/index.html?name2=John+Doe&email2=johndoe%40example.com&message2=This+is+a+test+message.
自从我将按钮类型更改为&#39;按钮&#39;而不是“提交”,按钮不会重新加载页面或给我这个网址。
答案 0 :(得分:0)
错误是语法错误,}
之后的echo "Your message has been sent successfully!";
。
修改强>
此外,请务必确认是否已调用$("#submit").click
。
您还需要取消表单提交操作(您使用ajax发送邮件)。
答案 1 :(得分:0)
要在PHP中阅读表格参数,您需要使用$ _GET。在该级别进行调试。看看你是否在服务器端获得这些值。另外,请确保您正在读取js中的所有值。提供HTML块会有所帮助。