0
我正在嘗試使用js和php做一個表單,所以,當我點擊按鈕時,它會一直保持在屏幕「發送電子郵件...」並且永遠不會發送,我檢查了控制檯,它說,錯誤是在我的PHP文件(404未找到錯誤)......如果有人能幫助我,我留下來使用的代碼如下:表格的問題
HTML:
<div class="contact-form">
<h3>Contate-nos</h3>
<form id="main-contact-form" name="contact-form" method="post" action="email.php">
<div class="form-group">
<input type="text" name="name" class="form-control" id="name" placeholder="Nome">
</div>
<div class="form-group">
<input type="email" name="email" class="form-control" id="email" placeholder="Email">
</div>
<div class="form-group">
<input type="text" name="subject" class="form-control" id="subject" placeholder="Assunto">
</div>
<div class="form-group">
<textarea name="message" class="form-control" rows="8" id="message" placeholder="Message"></textarea>
</div>
<button type="submit" name="submit" class="btn btn-primary">Send Message</button>
</form>
</div>
JS:
<script>
var form = $('#main-contact-form');
form.submit(function(event){
event.preventDefault();
var form_status = $('<div class="form_status"></div>');
var data = {
name: $('#name').val(),
email: $('#email').val(),
subject: $('#subject').val(),
message: $('#message').val()
};
$.ajax({
url: $(this).attr('action'),
type: "POST",
dataType: "json",
data: {'data': data},
beforeSend: function(){
form.prepend(form_status.html('<p><i class="fa fa-spinner fa-spin"></i> Sending email...</p>').fadeIn());
},
success: function (data) {
if(data == 1){
form_status.html('<p class="text-success">Sucessful!</p>').delay(3000).fadeOut();
}
else{
form_status.html('<p class="text-error">Sorry, try again later!</p>').delay(3000).fadeOut();
}
},
error: function (xhr, status, error) {
console.log(error);
}
});
});
</script>
PHP:
<?php
$data = $_POST['data'];
$name = $data['name'];
$from = $data['email'];
$subject = $data['subject'];
$message = $data['message'];
$to = '[email protected]';
$headers = "MIME-Version: 1.0";
$headers .= "Content-type: text/plain; charset=iso-8859-1";
$headers .= "From: {$name} <{$from}>";
$headers .= "Reply-To: <{$from}>";
$headers .= "Subject: {$subject}";
$headers .= "X-Mailer: PHP/".phpversion();
if(mail($to, $subject, $message, $headers)){
echo 1;
}
else{
echo 0;
}
$數據的一些其他或自頁面= $ _ POST [ '數據'];這是錯誤的,你的值將在$ _POST ['名稱']等 – miknik
試圖做到這一點,但仍然不工作 – darknone