我有一個表單用於在我的頁面上提交電子郵件,我無法找到是什麼原因導致它回滾失敗的提交。以下是關於表單的代碼部分。我繞過了JS,直接使用PHP腳本和郵件發送,所以這似乎不是問題。有任何想法嗎?電子郵件提交表單錯誤
JS:
$('#button-send').click(function(event){
$('#button-send').html('Sending E-Mail...');
event.preventDefault();
$('html, body').scrollTo($('#contact'), 'fast');
$.ajax({
type: 'POST',
url: 'send_form_email.php',
data: $('#contact_form').serialize(),
success: function(html) {
if(html.success == '1')
{
$('#button-send').html('Send E-Mail');
$('#success').show();
}
else
{
$('#button-send').html('Send E-Mail');
$('#error').show();
}
},
error: function(){
$('#button-send').html('Send E-Mail');
$('#error').show();
}
});
});
});
function valemail(email) {
var re = /^(([^<>()[\]\\.,;:\[email protected]\"]+(\.[^<>()[\]\\.,;:\[email protected]\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
return re.test(email);
}
這裏是PHP代碼:
<?php
// EDIT THE 2 LINES BELOW AS REQUIRED
$send_email_to = "[email protected]";
$email_subject = "New Message";
function send_email($name,$email,$email_message)
{
global $send_email_to;
global $email_subject;
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
$headers .= "From: ".$email. "\r\n";
$message = "<strong>Email = </strong>".$email."<br>";
$message .= "<strong>Name = </strong>".$name."<br>";
$message .= "<strong>Message = </strong>".$email_message."<br>";
@mail($send_email_to, $email_subject, $message,$headers);
return true;
}
function validate($name,$email,$message)
{
$return_array = array();
$return_array['success'] = '1';
$return_array['name_msg'] = '';
$return_array['email_msg'] = '';
$return_array['message_msg'] = '';
if($email == '')
{
$return_array['success'] = '0';
$return_array['email_msg'] = 'email is required';
}
else
{
$email_exp = '/^[A-Za-z0-9._%-][email protected][A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if(!preg_match($email_exp,$email)) {
$return_array['success'] = '0';
$return_array['email_msg'] = 'enter valid email.';
}
}
if($name == '')
{
$return_array['success'] = '0';
$return_array['name_msg'] = 'name is required';
}
else
{
$string_exp = "/^[A-Za-z .'-]+$/";
if (!preg_match($string_exp, $name)) {
$return_array['success'] = '0';
$return_array['name_msg'] = 'enter valid name.';
}
}
if($message == '')
{
$return_array['success'] = '0';
$return_array['message_msg'] = 'message is required';
}
else
{
if (strlen($message) < 2) {
$return_array['success'] = '0';
$return_array['message_msg'] = 'enter valid message.';
}
}
return $return_array;
}
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$return_array = validate($name,$email,$message);
if($return_array['success'] == '1')
{
send_email($name,$email,$message);
}
header('Content-type: text/json')
echo json_encode($return_array);
die();
?>
和我的頁面上的HTML:
<form method="post" action="send_form_email.php" id="contactform">
<div>
<label for="name">Enter your name</label>
<input type="text" class="input-field" id="name" name="name" value="">
</div>
<div>
<label for="email">Enter your e-mail</label>
<input type="text" class="input-field" id="email" name="email" value="">
</div>
<div>
<label>Type Your Message</label>
<textarea id="message" name="message"></textarea>
</div>
<a id="button-send" href="#" title="Send Email" class="button" style="width:100%;">Send E-Mail</a>
<div id="success">Your message has been successfully!</div>
<div id="error">Unable to send your message, please try later.</div>
</form>
你是什麼意思「踢回不成功的提交」?我建議從mail()調用中刪除錯誤抑制(函數前面的@符號)。另外,什麼是操作系統?它與PHP郵件有所不同。 – 2015-03-19 00:48:52
Ubuntu 14.從前端提交表單時,顯示「無法發送消息,請稍後再試。」正如我所提到的,我通過用提交類型輸入替換當前按鈕來測試php腳本,並在調用php腳本之後發送郵件。我有很少的Java經驗,所以我很難確定使用ajax時出現問題的地方。 – cassiusclay 2015-03-19 01:03:07
我在我的apache日誌中發現了以下錯誤 PHP解析錯誤:語法錯誤,意外'頭'(T_STRING)在/var/www/html/send_form_email.php在線80 – cassiusclay 2015-03-19 03:15:58