[英]Reloading the page (send form)
我在重新加載頁面時遇到問題。 單擊提交表單后,頁面會重新加載。 我單擊發送表單並將我帶到 http://localhost:3000/mail.php。 我希望該網站不被重新加載。 (我使用驗證表單 jquery 插件)。這是我的代碼:jquery
(function(){
$("#contactForm").on('submit', function(e) {
e.preventDefault();
var data = {
name = $('#field-name').val(),
phone = $('#field-phone').val(),
email = $('#field-email').val(),
message = $('#field-message').val()
};
$.ajax({
type: "POST",
url: "mail.php",
data: data,
success: function(){
console.log("jej");
}
});
return false;
});
});
和php代碼
<?php
$to = 'name@gmail.com';
$name = $_POST['name'];
$phone = $_POST['phone'];
$email= $_POST['email'];
$text = $_POST['message'];
$subject = 'Nowy e-mail od ' . $name . ' (' . $email . ')';
$message = $name . $phone . $email . $text;
$headers = 'From: ' . $name . "\r\n" .
if(mail($to, $subject, $message, $headers)) {
print "<p class='success'>Mail Sent.</p>";
} else {
print "<p class='Error'>Problem in Sending Mail.</p>";
}
?>
更改部分代碼
$("#contactForm").on('submit', function(e)
到這...
$("#contactFormButton").on('click', function(e)
並將您的提交按鈕更改為此...
<button id="contactFormButton" type="button" class="form-control">Submit</button>
在這里你的 php 代碼...
if(mail($to, $subject, $message, $headers)) {
echo "Mail Sent!";
} else {
echo "Error!!";
}
在這里你可以捕捉你的 php 代碼的結果......
success: function(data){
console.log(data);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.