wordpress上有多个ajax表单

时间:2014-10-25 13:54:43

标签: ajax wordpress

我的wordpress网站上已经有一个Ajax表单。现在,我需要第二个。所以我复制了Function.php中的函数。但它确实有效。

第一种形式是联系。第二个是题词。第一个很棒。 但是对于第二个,当我尝试发送时没有任何事情发生......

这是我的代码。

    add_action( 'wp_ajax_contact', '_ajax_contact' );
    add_action( 'wp_ajax_nopriv_contact', '_ajax_contact' );

    function _ajax_contact() {

/*-----------------------------------------------------------------------------------*/
/*  On vérifie le nonce de sécurité
/*-----------------------------------------------------------------------------------*/

check_ajax_referer( 'ajax_contact_nonce', 'security' );


/*-----------------------------------------------------------------------------------*/
/*  Protection des variables
/*-----------------------------------------------------------------------------------*/

$subject = wp_strip_all_tags( $_POST['subject'] ); // Sujet du message
$name = wp_strip_all_tags( $_POST['name'] ); // Nom de l'expéditeur
$sender = sanitize_email( $_POST['email'] ); // Adresse e-mail de l'expéditeur
$message = nl2br( stripslashes( wp_kses( $_POST['message'], $GLOBALS['allowedtags'] ) ) );


/*-----------------------------------------------------------------------------------*/
/*  Gestion des headers
/*-----------------------------------------------------------------------------------*/

$headers = array();
$headers[] = 'FROM : ' . $name . ' <' . $sender .'>' . "\r\n";


/*-----------------------------------------------------------------------------------*/
/*  Gestion du message
/*-----------------------------------------------------------------------------------*/

ob_start();
include( get_template_directory() . '/inc/mail/contact.php' );
$mail = ob_get_contents();
ob_end_clean();


/*-----------------------------------------------------------------------------------*/
/*  Envoie de l'e-mail
/*-----------------------------------------------------------------------------------*/


// Support d'un contenu HTML dans l'email
add_filter( 'wp_mail_content_type', create_function('', 'return "text/html";') );

if( wp_mail( 'emailtest@gmail.com', '[subject] Contact', $mail, $headers ) ) {

    // Tout est ok, on avertit l'utilisateur
    wp_send_json( 'success' );

}
else {

    // Il y a une erreur avec le mail, on avertit l'utilisateur
    wp_send_json( 'error' );
        }


     }  

               /*-----------------------------------------------------------------------------------*/
/*  Second form
/*-----------------------------------------------------------------------------------*/

  add_action( 'wp_ajax_inscription', '_ajax_inscription' );
  add_action( 'wp_ajax_nopriv_inscription', '_ajax_inscription' );

  function _ajax_inscription() {

/*-----------------------------------------------------------------------------------*/
/*  On vérifie le nonce de sécurité
/*-----------------------------------------------------------------------------------*/

check_ajax_referer( 'ajax_inscription_nonce', 'security' );


/*-----------------------------------------------------------------------------------*/
/*  Protection des variables
/*-----------------------------------------------------------------------------------*/
$subject = wp_strip_all_tags( $_POST['subject'] ); // Sujet du message
$name = wp_strip_all_tags( $_POST['name'] ); // Nom de l'expéditeur
$sender = sanitize_email( $_POST['email'] ); // Adresse e-mail de l'expéditeur
$message = nl2br( stripslashes( wp_kses( $_POST['message'], $GLOBALS['allowedtags'] ) ) );


/*-----------------------------------------------------------------------------------*/
/*  Gestion des headers
/*-----------------------------------------------------------------------------------*/

$headers = array();
$headers[] = 'FROM : ' . $name . ' <' . $sender .'>' . "\r\n";


/*-----------------------------------------------------------------------------------*/
/*  Gestion du message
/*-----------------------------------------------------------------------------------*/

ob_start();
include( get_template_directory() . '/inc/mail/contact.php' );
$mail = ob_get_contents();
ob_end_clean();


/*-----------------------------------------------------------------------------------*/
/*  Envoie de l'e-mail
/*-----------------------------------------------------------------------------------*/


// Support d'un contenu HTML dans l'email
add_filter( 'wp_mail_content_type', create_function('', 'return "text/html";') );

if( wp_mail( 'testemail@gmail.com', $subject, $mail, $headers ) ) {

    // Tout est ok, on avertit l'utilisateur
    wp_send_json( 'success' );

}
else {

    // Il y a une erreur avec le mail, on avertit l'utilisateur
    wp_send_json( 'error' );
}

}

2 个答案:

答案 0 :(得分:1)

查看您的代码,我要做的第一件事是使用name='name'重命名字段。我编写了很多表格,几乎在所有情况下,WP都不会发布我的表单,因为它保留了name='name'字段。

此外,激活WP_DEBUG模式以显示所有错误,并尝试使用Firebug或浏览器控制台查找JavaScript错误。

答案 1 :(得分:1)

感谢您的回答,最后我找到了另一个解决方案。问题不在这个文件中..

它在我的表单文件中。我忘了在发送按钮上更改值。

我的错误:

<input type="hidden" name="action" value="contact" />
            <?php wp_nonce_field( 'ajax_inscription_nonce', 'security' ); ?>
            <input id="send-message" type="submit" value="Envoyer">

好的代码

<input type="hidden" name="action" value="inscription" />
            <?php wp_nonce_field( 'ajax_inscription_nonce', 'security' ); ?>
            <input id="send-message" type="submit" value="Envoyer">

非常感谢。

相关问题