【发布时间】:2018-07-21 14:49:52
【问题描述】:
我目前正在制作一个 WordPress 联系表单,它是作为插件创建的。我需要提交后的表单来发送电子邮件而无需重新加载页面。重新加载时一切正常。问题是当按下提交按钮并且 e.preventDefault() 处于活动状态时,浏览器会显示 500 (Internal Server Error) 和 admin-ajax.php = 0
P.S 帖子已编辑
<?php
add_action( 'admin_footer', 'html_form_code' ); // Write our JS below here
//
function html_form_code() {
?>
<form action="<?php esc_url( $_SERVER['REQUEST_URI'] )?>" method="post" class="contact-form" id="contact-form" >
<div class=header-contact>
<p><h2>Contact Form</h2></p>
<hr>
</div>
<div class=input-containers>
<input type="text" id="name" name="cf-name" pattern="[a-zA-Z0-9 ]+" value="" size="40" placeholder="Име и фамилия"/>
</div>
<div class=input-containers>
<input type="email" id="email" name="cf-email" value="" size="40" placeholder="Поща"/>
</div>
<div class=input-containers>
<input type="text" id="subject" name="cf-subject" pattern="[a-zA-Z ]+" value="" size="40" placeholder="Относно"/>
</div>
<div class=input-containers>
<textarea rows="10" id="message" cols="35" name="cf-message" placeholder="Текст"></textarea>
</div>
<div class=input-containers>
<input type="submit" name="cf-submitted" value="Send" id="submitForm">
</div>
<p id="verify" style="display:none;">Your message has been sent.<br /><br /></p>
</form>
<script>
jQuery('#contact-form').submit(function(e){
e.preventDefault();
var name = jQuery('#name').val();
var email = jQuery('#email').val();
var subject = jQuery('#subject').val();
var message = jQuery('#message').val();
jQuery.ajax({
url: '<?php echo admin_url('admin-ajax.php'); ?>',
type: "POST",
data:{
action: 'send_email',
name: name,
email: email,
subject: subject,
message: message,
},
success:function(res){
alert("Email Sent.");
}
});
});
</script>
<?php
}
add_action( 'wp_ajax_send_email', 'deliver_mail' );
add_action( 'wp_ajax_nopriv_send_email', 'deliver_mail' );
function deliver_mail() {
require_once "wp-includes/class-phpmailer.php";
if (isset($_POST["cf-submitted"])) {
// sanitize form values
$name = sanitize_text_field( $_POST["cf-name"] );
$email = sanitize_email( $_POST["cf-email"] );
$subject = sanitize_text_field( $_POST["cf-subject"] );
$message = esc_textarea( $_POST["cf-message"] );
// get the blog administrator's email address
//$to = "email@gmx.com";
$headers = "From: $name <$email>" . "\r\n";
// Localhost
$mail = new PHPMailer(true);
$mail->IsSMTP(); // telling the class to use SMTP
$mail->CharSet = 'UTF-8';
$mail->SMTPDebug = 0; // enables SMTP debug information (for testing)
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = "ssl"; // sets the prefix to the servier
$mail->Host = "mail.gmx.com"; // sets GMX as the SMTP server for example: mail.gmx.com
$mail->Port = 465; // set the SMTP port for the GMX server
$mail->Username = $email;
$mail->Password = 'pass';
$mail->SetFrom($email, $name);
$mail->AddAddress($email);
$mail->Subject = $subject;
$mail->MsgHTML($message);
$headers .= "Content-Type: text/html; charset=utf-8";
$headers .= "Content-Transfer-Encoding: 8bit";
try {
$mail->send();
$msg = "An email has been sent for verfication.";
$msgType = "success";
wp_safe_redirect( home_url(), 302 );
exit();
} catch (Exception $ex) {
$msg = $ex->getMessage();
$msgType = "warning";
wp_safe_redirect( home_url(), 302 );
exit();
}
add_action( 'init', function() {
if ( ! empty( $_POST['form_submitted'] ) ) {
deliver_mail();
}
});
function cf_shortcode() {
ob_start();
//deliver_mail();
html_form_code();
//ob_end_flush();
return ob_get_clean();
}
add_shortcode( 'contact_form_second', 'cf_shortcode' );
【问题讨论】:
-
尝试“返回假;”在提交操作中的 ajax 调用定义之后。
-
您没有发布任何数据,您需要将其发送到设置为处理处理的正确 wordpress ajax 端点
-
您的
$.ajax没有发送任何内容。需要添加表单数据:data: $('#contact-form').serialize(); -
问题不在于发送数据。有没有它都是一样的。 P.S 我编辑了代码。
-
那里出现语法错误:
data: $('#contact-form').serialize();- 将;更改为,(即逗号)。 问题不在于发送数据。 - 实际上是的,这是其中一个问题。 有无它都一样。 - 有它,你仍然需要将url更改为admin-ajax.php,并发送正确的'action',在你的情况下是my_action。所以试试url: "<?php echo admin_url( 'admin-ajax.php?action=my_action' ); ?>",