phpajaxwordpress

More than one ajax form on wordpress


i have already one Ajax form on my wordpress website. Now, i need a second one. So i duplicate the function in Function.php. But it does'nt work.

The first form is contact. The second one is inscription. The first one work great. But for the second one, nothing happen when i try to send...

Here my code.

    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' );
}

}


Solution

  • Thank for your answer, finally i have found another solution. The problem was not in this file..

    It was on my form file. I forgot to change a value on the send button.

    My mistake :

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

    The good code

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

    Thanks a lot.