當我用我的聯繫表格發送電子郵件時,textarea值不會發送,並且電子郵件中的「message:」字段始終爲空。聯繫表格發送帶空信息字段的郵件
在jquery中手動聲明輸入變量而不是使用序列化給出了相同的結果。
textarea是在窗體標籤內,所以我不知道爲什麼它的價值沒有拿起和張貼。
我試着給textarea一個id =「消息」,並在jQuery中手動調用它:var text = $("#message").val();
和var text = $("textarea#message").val();
但它仍然不會發送。
我不知道我在做什麼錯。請幫忙。
這裏是我的代碼:
<form id="contactForm" method="POST">
<div class="row small-margin">
<div class="col-md-4">
<i class="pe-7s-user pe-2x icon-contact"></i>
<input name="name" type="text" placeholder="Name(required)" required size="35">
</div>
<div class="col-md-4">
<i class="pe-7s-mail pe-2x icon-contact"></i>
<input name="email" type="email" placeholder="Email(required)" required size="35">
</div>
<div class="col-md-4">
<i class="pe-7s-link pe-2x icon-contact"></i>
<input name="website" type="text" placeholder="Website" size="35">
</div>
</div>
<div class="row small-margin">
<div class="col-md-12">
<!-- Message Field -->
<textarea name="message" placeholder="Your Message(required)" required></textarea>
<!-- Submit Button -->
<button id="submit" name="submit" type="submit" class="button" value="submit">SEND MESSAGE</button>
<!-- Success Message -->
<div id="msgSubmit" class="alert alert-success text-center hidden">Message Sent Successfully</div>
</div>
</div>
</form>
和JS:
$("#contactForm").submit(function(event){
// cancels the form submission
event.preventDefault();
console.log($(this).serialize());
});
$.ajax({
type: "POST",
url: "php/form-process.php",
data: "name=" + name + "&email=" + email + "&website=" + website + "&message=" + text,
success : function(text){
if (text == "success"){
formSuccess();
}
}
});
}
function formSuccess(){
$("#msgSubmit").removeClass("hidden");
}
和PHP:
<?php
// Set variables
$name = $_POST['name'];
$email = $_POST['email'];
$website = $_POST['website'];
$message = $_POST['text'];
$to = '[email protected]';
$subject = 'New Message from Nixo';
// Prepare email body text
$Body .= "Name: ";
$Body .= $name;
$Body .= "\n";
$Body .= "Email: ";
$Body .= $email;
$Body .= "\n";
$Body .= "Website: ";
$Body .= $website;
$Body .= "\n";
$Body .= "Message: ";
$Body .= $message;
$Body .= "\n";
// Send email
$success = mail($to, $subject, $Body, "From:".$email);
if ($success){
echo "success";
}else{
echo "<p>Something went wrong, please try again!</p>";
}
?>
那麼..消息表單元素的名稱是「消息」,你在你的AJAX調用中使用「text」:「」&message =「+ text,」。 –
我用郵件替換了文本,現在我在電子郵件中收到了「郵件:udefined」。 – Novakim