나는 ajax와 함께 Wordpress에 대한 문의 양식을 쓰려고합니다. jquery가 확인을 추가하지 않기 때문에 성공에 문제가 있습니다.ajax와 양식 성공을 확인하지
PHP 함수
add_action('wp_ajax_submit_form', 'submit_form_callback');
add_action('wp_ajax_nopriv_submit_form', 'submit_form_callback');
function submit_form_callback(){
$params = array();
parse_str($_POST['data'], $params);
$name = sanitize_text_field (trim($params['name_contact']));
$email = sanitize_email($params['email_contact']);
$message = wp_kses_data($params['message_contact']);
$subject = __('e-mail from ', 'ddflatro').get_bloginfo('url') ;
$site_owners_email = get_bloginfo('admin_email');
if ($name=="") {
$error['name'] = __('Please enter your name', 'ddflatro');
}
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email)) {
$error['email'] = __('Please enter a valid email address', 'ddflatro');
}
else
{
$mailcheck = spamcheck($email);
if ($mailcheck==FALSE) {
$error['email'] = __('Please enter a valid email address', 'ddflatro');
}
}
if ($message== "") {
$error['message'] = __('Please leave a message', 'ddflatro');
}
if (!$error) {
$success['success'] = __('Yeah', 'ddflatro');
echo json_encode($success);
$headers[] = 'From: ' . $name . ' <' . $email . '>' . "\r\n";
$headers[] = 'Content-type: text/html' . "\r\n";
wp_mail($site_owners_email, $subject, $message, $headers);
} # end if no error
else {
echo json_encode($error);
}
die(); // this is required to return a proper result
}
jQuery를
$("#contact-ddf").submit(function(event) {
event.preventDefault();
$('#response').empty();
var data = {
action: 'submit_form',
data: $(this).serialize()
};
$.post(ajaxurl, data, function(response) {
console.log(response);
if(response.success){
$('#response').append(response.success);
}
alert(response);
if(response.name){
$('#response').append(response.name + "<br>");
}
if(response.email){
$('#response').append(response.email + "<br>");
}
if(response.message){
$('#response').append(response.message + "<br>");
}
if(response.subject){
$('#response').append(response.subject + "<br>");
}
},'json');
});
만약 통지를 추가 이름이나 이메일 또는 메시지 jQuery를 누락하지만 이메일이 때 아무것도 보내 우연히 있다.
나요 너 알아 낸거야? – ernstl