[英]Contact form (Wordpress) won't submit
我正在使用Akismet表單和jQuery AJAX腳本在Wordpress中創建主題。 我已經修改了所有相關區域以集成到我的Wordpress網站中,甚至使用已經存在的Akismet API密鑰。 這是我的代碼:
形成
<form method="post" id="contact_form">
<input id="name" type="text" name="name" tabindex="1"/> <label for="name">Name</label>
<input id="email" type="text" name="email" tabindex="2"/> <label for="email">E-mail</label>
<input id="website" type="text" name="website" tabindex="3" value="http://" /> <label for="website">Website</label>
<textarea id="message" tabindex="4" rows="10" cols="60" name="message"></textarea>
<input type="submit" value="Send E-mail" tabindex="5" />
</form>
jQuery的
<script>
$(function() {
$("#contact_form").submit(function() {
$.ajax({
type: "POST",
url: "<?php bloginfo('template_url'); ?>/inc/email.php",
data: $(form).serialize(),
success: function(){
$('.success').fadeIn(1000);
}
});
return false;
});
});
</script>
PHP腳本
<?php
require "Akismet.class.php";
function send_mail( $name, $email, $website, $ip, $is_spam, $message) {
$subject = '';
if( $is_spam == true )
$subject = "[SPAM?]";
$subject .= "[Your_site.com] E-mail received from ".$author_name."//".$author_email."//".$ip;
wp_mail( get_option('admin_email'), $subject, $message, $name.' <'.$email.'>');
}
if(isset($_POST['action'])) {
$wp_key = get_option( 'wordpress_api_key' );
$our_url = get_bloginfo( 'url' );
$name = $_POST['name'];
$email = $_POST['email'];
$website = $_POST['website'];
$message = $_POST['message'];
$ip = $_SERVER['REMOTE_ADDR'];
$akismet = new Akismet($our_url, $wp_key);
$akismet->setCommentAuthor($name);
$akismet->setCommentAuthorEmail($email);
$akismet->setCommentAuthorURL($website);
$akismet->setCommentContent($message);
$akismet->setUserIP($ip);
send_mail( $name, $email, $website, $ip, $akismet->isCommentSpam(), $message);
}
當我提交表單時,即使URL與聯系頁面相同,它也會處理某些內容並將我重定向到404頁面。 該電子郵件也不會發送。 有人可以幫助我解決問題所在嗎?
您的表單將在ajax調用進入之前提交。使用preventDefault
停止默認操作
$(function() {
$("#contact_form").submit(function(e) {
e.preventDefault();
...
編輯
我只是注意到您返回false, 應該可以達到相同的結果,但是有時我已經注意到它沒有
您可以嘗試preventDefault()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.