我在我的Wordpress網頁上有一個動態聯繫表單。爲什麼當在Wordpress上使用Ajax傳遞參數時出現「500 Internal Server Error」錯誤?
這是HTML結構:
<div class="form_content">
<div> Name: <span> <input type="text" id="fullname"> </span></div>
<div> E-mail: <span> <input type="text" id="email"> </span></div>
<div> Message: <span> <input type="text" id="text"> </span></div>
<div id="sendBtn"> Submit </div>
</div>
我有一個從HTML抓住輸入,並將其發送到content.php文件我有我的twentyfifteen,兒童主題的功能。
// Send button for the "contact form".
jQuery('#sendBtn').click(function(){
//get info
var fullname = jQuery("#fullname").val();
var email = jQuery("#email").val();
var text = jQuery("#text").val();
//send info to php
jQuery.ajax({
beforeSend: function() {
if (IsEmail(email) == false) {
jQuery('#aboutUnsuccess').show("slow");
/*jQuery('#pixel-thing').show("fast");*/
jQuery('.form_content').hide("slow");
}
},
url: 'http://54.149.xx.xx/wp-content/themes/twentyfifteen-child/contact.php',
type: "POST",
data: ({ "fullname": fullname, "email": email, "text": text }),
success: function (results){
if (IsEmail(email) == true) {
//hide table
jQuery('.form_content').hide('slow', function() {
jQuery('.form_content').hide("slow");
});
//show textboxes
jQuery('#aboutSuccess').show("slow");
jQuery("#aboutSuccess").append("<iframe id=\"pixel-thing\" src=\"http://54.149.xx.xx/wp-content/themes/twentyfifteen-child/thePixel.html\" width=\"1\" height=\"1\" border=\"0\"></iframe>");
/************************************/
window.google_trackConversion({
google_conversion_id: 666,
google_remarketing_only: true
});
/*************************************/
}
}
});
最後我content.php處理參數和相關信息發送到我的郵箱:
<?php
require_once "Mail.php";
if(isset($_POST['email'])) {
$email = $_POST['email'];
$email_to = "[email protected]";
$host = "ssl://smtp.gmail.com:465";
$username = '[email protected]';
$password = 'passpaspaspas';
$email_subject = "You have a new email from $email via example.com website";
$message = $_POST['text'];
$headers = array ('From' => $email, 'To' => $email_to,'Subject' => $email_subject);
$smtp = Mail::factory('smtp',
array ('host' => $host,
'auth' => true,
'username' => $username,
'password' => $password));
$mail = $smtp->send($email_to, $headers, $message);
if (PEAR::isError($mail)) {
echo($mail->getMessage());
} else {
echo("Message successfully sent!\n");
}
}
我感動我的網站,這是沒有任何MVC/CMS構建,使用WordPress。這適用於我的「常規」網站。
爲什麼我的代碼在wordpress中無法使用?
這是鉻合金「網絡」輸出:
Remote Address:54.159.xx.xx:80
Request URL:http://54.159.xx.xx/wp-content/themes/twentyfifteen-child/contact.php
Request Method:POST
Status Code:500 Internal Server Error
Request Headersview source
Accept:*/*
Accept-Encoding:gzip, deflate
Accept-Language:en-US,en;q=0.8
Connection:keep-alive
Content-Length:63
Content-Type:application/x-www-form-urlencoded; charset=UTF-8
Cookie:wp-settings-1=editor%3Dtinymce%26posts_list_mode%3Dlist; wp-settings-time-1=1424359234
Host:54.149.xx.xx
Origin:http://54.149.xx.xx
Referer:http://54.149.xx.xx/?page_id=73
User-Agent:Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.115 Safari/537.36
X-Requested-With:XMLHttpRequest
Form Dataview sourceview URL encoded
fullname: Test
email:[email protected]
text:This is a test
Response Headersview source
Connection:close
Content-Length:0
Content-Type:text/html
Date:Tue, 24 Feb 2015 14:13:46 GMT
Server:Apache/2.4.7 (Ubuntu)
X-Powered-By:PHP/5.5.9-1ubuntu4.5
感謝您的關注埃裏克:) 讓我挖掘到了一段時間..:d – 2015-02-24 15:01:53
因爲我不是那麼熟悉WordPress的,我會坐在自己的明天和閱讀如何構建插件,因爲我沒有任何線索你發送的鏈接中發生了什麼,無論如何,我必須學會這樣做的權利。 之前的一個小問題,我把「contact.php」的內容移到了一個函數中。我在.js文件中寫入什麼,而不是這行:'url:'http://54.149.xx.xx/wp-content/themes/twentyfifteen-child/contact.php', '? – 2015-02-24 15:40:24
我可以想象,學習曲線有時會很陡:) 您必須使用'admin-ajax.php',但使用ajax_object.ajax_url會更好,因爲您可以在Wordpress中動態設置正確的URL。這個鏈接是一個更詳細的讓你走:http://codex.wordpress.org/AJAX_in_Plugins – 2015-02-24 15:44:34