來自初學者的另一個愚蠢的問題。我有這樣的功能:發送數據的PHP和AJAX問題
$(document).ready(function() {
$('#submit-form').click(function(e){
e.preventDefault();
var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
var name = $('#name').val(),
email = $('#email').val(),
phone = $('#phone').val(),
date = $('#date').val(),
message = $('#message').val(),
data_html,
success = $('#success');
if(name == "")
$('#name').val('Please enter your name.');
if(phone == "")
$('#phone').val('Please enter your phone number.');
if(date == "")
$('#date').val('Please enter a date and time.');
if(email == ""){
$('#email').val('Your email is required.');
}else if(reg.test(email) == false){
$('#email').val('Invalid Email Address.');
}
if(message == "")
$('#message').val('Message is required.');
if(message != "" && name != "" && reg.test(email) != false) {
data_html = "name=" + name + "&email="+ email + "&message=" + message + "&phone="+ phone + "&date="+ date;
//alert(data_html);
$.ajax({
type: 'POST',
url: '../contact_form.php',
data: data_html,
success: function(msg){
if (msg == 'sent'){
success.html('<div class="alert alert-success">Message <strong>successfully</strong> sent!</div>') ;
$('#name').val('');
$('#phone').val('');
$('#email').val('');
$('#date').val('');
$('#message').val('');
}else{
success.html('<div class="alert alert-error">Message <strong>NOT</strong> sent! Please try again later. </div>') ;
}
}
});
}
return false;
});
});
我創造了這個PHP這可能是錯誤的,所以請不要評判我......我仍然在學習:)我是一個總初學者到這個所以請不要給我很難:)
<?php
$to = '[email protected]';
$subject = 'Request a Booking';
if($to) {
$name = $_POST['name'];
$email = $_POST['email'];
$fields = array(
0 => array(
'text' => 'Name',
'val' => $_POST['name']
),
1 => array(
'text' => 'Email address',
'val' => $_POST['email']
),
2 => array(
'text' => 'Phone',
'val' => $_POST['phone']
),
3 => array(
'text' => 'Date & Time',
'val' => $_POST['date']
),
4 => array(
'text' => 'Message',
'val' => $_POST['message']
)
);
$message = "";
foreach($fields as $field) {
$message .= $field['text'].": " . htmlspecialchars($field['val'], ENT_QUOTES) . "<br>\n";
}
ini_set("SMTP","aspmx.l.google.com");
$headers = "MIME-Version: 1.0\r\nContent-type: text/html; charset=utf-8\r\n";
$headers .= "From: \"" . $name . "\" \r\n";
$headers .= "Reply-To: " . $email . "\r\n";
$message = utf8_decode($message);
mail($to, $subject, $message, $headers);
?>
我不斷收到錯誤消息不發送該消息。它不與我的PHP連接,我認爲。有什麼建議?
你已經安裝了郵件服務器? – krishna
沒有我只是想把它發送到我自己的電子郵件地址。 – user2976554
按f12查看控制檯窗口它顯示任何錯誤 –