简体   繁体   中英

Unable to submit my wordpress contact form

I wanted to create a wordpress extension to display a contact form on a website I made. I am a beginner with wordpress and I can't find a solution to my problem. The form doesn't send, I have the error message I created in my deliver_mail() function that always displays, but I don't understand why? Here is my code. Thanks for your answers!

<?php

// FORMULAIRE DE CONTACT HTML
// ##########################
function html_form_code() {

    echo ('
    <section class="section-form d-flex flex-column justify-content-center align-items-center">
        <div class="container text-center mt-5">
            <h2 class="fs-3">Une question, une demande de devis&nbsp;?</h2>
            <p>Remplissez le formulaire ci-dessous, nous vous recontacterons rapidement&nbsp;!</p>
        </div>
        <form action="  '. esc_url( $_SERVER['REQUEST_URI'] ) .'" method="post" class="container mt-3 p-4">
            <div class="form-floating mb-3">
                <input type="text" class="form-control" name="cf-name" pattern="[a-zA-Z0-9 ]+" value="' . ( isset( $_POST["cf-name"] ) ? esc_attr( $_POST["cf-name"] ) : '' ) . ' " size="40"/>
                <label for="floatingInput">Nom</label>
            </div>
            <div class="form-floating mb-3">
                <input type="text" class="form-control" name="cf-firstname" value=" ' . ( isset( $_POST["cf-firstname"] ) ? esc_attr( $_POST["cf-firstname"] ) : '' ) . ' " size="40" >
                <label for="floatingInput">Prénom</label>
            </div>
            <div class="form-floating mb-3">
                <input type="email" class="form-control" name="cf-email" value=" ' . ( isset( $_POST["cf-email"] ) ? esc_attr( $_POST["cf-email"] ) : '' ) .' " size="40" >
                <label for="floatingInput">Email</label>
            </div>
            <div class="form-floating mb-3">
                <input type="tel" class="form-control" name="cf-tel" value="' . ( isset( $_POST["cf-tel"] ) ? esc_attr( $_POST["cf-tel"] ) : '' ) . ' " size="40" >
                <label for="floatingInput">Téléphone</label>
            </div>
            <div class="form-floating mb-3">
                <input type="text" class="form-control" name="cf-address" value="' . ( isset( $_POST["cf-address"] ) ? esc_attr( $_POST["cf-address"] ) : '' ) . '" size="250" >
                <label for="floatingInput">Adresse</label>
            </div>
            <div class="form-floating mb-3">
                <input type="text" class="form-control" name="cf-postal" value="  ' . ( isset( $_POST["cf-postal"] ) ? esc_attr( $_POST["cf-postal"] ) : '' ) . '" size="10" >
                <label for="floatingInput">Code postal</label>
            </div>
            <div class="form-floating mb-3">
                <input type="text" class="form-control"name="cf-city" value="' . ( isset( $_POST["cf-city"] ) ? esc_attr( $_POST["cf-city"] ) : '' ) . '" size="250" >
                <label for="floatingInput">Ville</label>
            </div>
            <div>
                <p class="fs-5 mt-4">Votre demande concerne&nbsp;:</p>
                <div class="form-check">
                    <input class="form-check-input" type="checkbox" name="cf-help" value="' . ( isset( $_POST["cf-help"] ) ? esc_attr( $_POST["cf-help"] ) : '' ) . '"/>
                    <label class="form-check-label" for="flexCheckDefault">
                        Les Aides de l\'État
                    </label>
                </div>
                <div class="form-check">
                    <input class="form-check-input" type="checkbox" name="cf-contract" value="' . ( isset( $_POST["cf-contract"] ) ? esc_attr( $_POST["cf-contract"] ) : '' ) . '">
                    <label class="form-check-label" for="flexCheckChecked">
                        Nos Contrats d\'Entretien
                    </label>
                </div>
                <div class="form-check">
                    <input class="form-check-input" type="checkbox" name="cf-quote" value="' . ( isset( $_POST["cf-quote"] ) ? esc_attr( $_POST["cf-quote"] ) : '' ) . '"/>
                    <label class="form-check-label" for="flexCheckChecked">
                        Une Demande de Devis
                    </label>
                </div>
                <div class="form-check">
                    <input class="form-check-input" type="checkbox" name="cf-other" value="' . ( isset( $_POST["cf-other"] ) ? esc_attr( $_POST["cf-other"] ) : '' ) . '"/>
                    <label class="form-check-label mb-4" for="flexCheckChecked">
                        Autre
                    </label>
                </div>
            </div>
            <div class="form-floating mb-3">
                <textarea class="form-control"></textarea>
                <label for="floatingInput">Votre message</label>
            </div>
            <button type="submit" class="btn button-primary mt-3" name="cf-submitted">Envoyer</button>
        </form>
    </section>') ;

}

// ENVOYER LES DONNEES A L ADMIN
// ##############################
function deliver_mail() {

    // if the submit button is clicked, send the email
    if ( isset( $_POST['cf-submitted'] ) ) {

        // sanitize form values
        $name = sanitize_text_field( $_POST["cf-name"] );
        $firstname = sanitize_text_field( $_POST["cf-firstname"] );
        $email   = sanitize_email( $_POST["cf-email"] );
        $tel = sanitize_text_field( $_POST["cf-tel"] );
        $address = sanitize_text_field( $_POST["cf-address"] );
        $postal = sanitize_text_field( $_POST["cf-postal"] );
        $city = sanitize_text_field( $_POST["cf-city"] );
        $message = esc_textarea( $_POST["cf-message"] );

        // get the blog administrator's email address
        $to = get_option( 'admin_email' );

        $headers = "From: $name <$email>" . "\r\n";

        // If email has been process for sending, display a success message
        if ( wp_mail( $to, $subject, $message, $headers ) ) {
            echo '<div>';
            echo '<p>Merci de votre envoi, nous vous recontacterons très vite !</p>';
            echo '</div>';
        } else {
            echo 'Echec lors de la validation du formulaire';
        }
    }

}

// APPELLEE LORSQUE LE SHORTCODE EST ACTIF
// #######################################
function cf_shortcode() {

    ob_start();
    deliver_mail();
    html_form_code();
    return ob_get_clean();

}

add_shortcode( 'sitepoint_contact_form', 'cf_shortcode' );



There's a few things wrong here. You don't have a Subject, and your textarea has no name. Also, you can make this one function/shortcode.

Also, instead of using the $_SERVER superglobal, I used the $wp->request instead.

function cf_shortcode() {
    ob_start();
    global $wp;
    echo ( '
    <section class="section-form d-flex flex-column justify-content-center align-items-center">
        <div class="container text-center mt-5">
            <h2 class="fs-3">Une question, une demande de devis&nbsp;?</h2>
            <p>Remplissez le formulaire ci-dessous, nous vous recontacterons rapidement&nbsp;!</p>
        </div>
        <form action="  ' . esc_url( home_url( $wp->request ) ) . '" method="post" class="container mt-3 p-4">
            <div class="form-floating mb-3">
                <input type="text" class="form-control" name="cf-name" pattern="[a-zA-Z0-9 ]+" value="' . ( isset( $_POST['cf-name'] ) ? esc_attr( $_POST['cf-name'] ) : '' ) . ' " size="40"/>
                <label for="floatingInput">Nom</label>
            </div>
            <div class="form-floating mb-3">
                <input type="text" class="form-control" name="cf-firstname" value=" ' . ( isset( $_POST['cf-firstname'] ) ? esc_attr( $_POST['cf-firstname'] ) : '' ) . ' " size="40" >
                <label for="floatingInput">Prénom</label>
            </div>
            <div class="form-floating mb-3">
                <input type="email" class="form-control" name="cf-email" value=" ' . ( isset( $_POST['cf-email'] ) ? esc_attr( $_POST['cf-email'] ) : '' ) . ' " size="40" >
                <label for="floatingInput">Email</label>
            </div>
            <div class="form-floating mb-3">
                <input type="tel" class="form-control" name="cf-tel" value="' . ( isset( $_POST['cf-tel'] ) ? esc_attr( $_POST['cf-tel'] ) : '' ) . ' " size="40" >
                <label for="floatingInput">Téléphone</label>
            </div>
            <div class="form-floating mb-3">
                <input type="text" class="form-control" name="cf-address" value="' . ( isset( $_POST['cf-address'] ) ? esc_attr( $_POST['cf-address'] ) : '' ) . '" size="250" >
                <label for="floatingInput">Adresse</label>
            </div>
            <div class="form-floating mb-3">
                <input type="text" class="form-control" name="cf-postal" value="  ' . ( isset( $_POST['cf-postal'] ) ? esc_attr( $_POST['cf-postal'] ) : '' ) . '" size="10" >
                <label for="floatingInput">Code postal</label>
            </div>
            <div class="form-floating mb-3">
                <input type="text" class="form-control"name="cf-city" value="' . ( isset( $_POST['cf-city'] ) ? esc_attr( $_POST['cf-city'] ) : '' ) . '" size="250" >
                <label for="floatingInput">Ville</label>
            </div>
            <div>
                <p class="fs-5 mt-4">Votre demande concerne&nbsp;:</p>
                <div class="form-check">
                    <input class="form-check-input" type="checkbox" name="cf-help" value="' . ( isset( $_POST['cf-help'] ) ? esc_attr( $_POST['cf-help'] ) : '' ) . '"/>
                    <label class="form-check-label" for="flexCheckDefault">
                        Les Aides de l\'État
                    </label>
                </div>
                <div class="form-check">
                    <input class="form-check-input" type="checkbox" name="cf-contract" value="' . ( isset( $_POST['cf-contract'] ) ? esc_attr( $_POST['cf-contract'] ) : '' ) . '">
                    <label class="form-check-label" for="flexCheckChecked">
                        Nos Contrats d\'Entretien
                    </label>
                </div>
                <div class="form-check">
                    <input class="form-check-input" type="checkbox" name="cf-quote" value="' . ( isset( $_POST['cf-quote'] ) ? esc_attr( $_POST['cf-quote'] ) : '' ) . '"/>
                    <label class="form-check-label" for="flexCheckChecked">
                        Une Demande de Devis
                    </label>
                </div>
                <div class="form-check">
                    <input class="form-check-input" type="checkbox" name="cf-other" value="' . ( isset( $_POST['cf-other'] ) ? esc_attr( $_POST['cf-other'] ) : '' ) . '"/>
                    <label class="form-check-label mb-4" for="flexCheckChecked">
                        Autre
                    </label>
                </div>
            </div>
            <div class="form-floating mb-3">
                <textarea class="form-control" name="cf-message"></textarea>
                <label for="floatingInput">Votre message</label>
            </div>
            <button type="submit" class="btn button-primary mt-3" name="cf-submitted">Envoyer</button>
        </form>
    </section>' );

    // if the submit button is clicked, send the email
    if ( isset( $_POST['cf-submitted'] ) ) {

        // sanitize form values
        $name      = sanitize_text_field( $_POST['cf-name'] );
        $firstname = sanitize_text_field( $_POST['cf-firstname'] );
        $email     = sanitize_email( $_POST['cf-email'] );
        $tel       = sanitize_text_field( $_POST['cf-tel'] );
        $address   = sanitize_text_field( $_POST['cf-address'] );
        $postal    = sanitize_text_field( $_POST['cf-postal'] );
        $city      = sanitize_text_field( $_POST['cf-city'] );
        $message   = sanitize_textarea_field( $_POST['cf-message'] );

        // get the blog administrator's email address
        $to = get_option( 'admin_email' );
        // Include Subject.
        $subject = 'Your Subject';
        // Headers is an array. You need to push to it.
        $headers[] = "From: $name <$email>" . "\r\n";
        // If email has been process for sending, display a success message
        if ( wp_mail( $to, $subject, $message, $headers ) ) {
            echo '<div>';
            echo '<p>Merci de votre envoi, nous vous recontacterons très vite !</p>';
            echo '</div>';
        } else {
            echo 'Echec lors de la validation du formulaire';
        }
    }
    return ob_get_clean();
}

add_shortcode( 'sitepoint_contact_form', 'cf_shortcode' );

I tested this, and it works... however, you're not including in the body of the email any of the form fields.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM