I have a probleme with a dev from one day so i post here. Maybe someoine have a solution.
I created à custom contact form in a template wordpress. Form is basic (name, surname…).
The form is in the template and send data via ajax to a process.php files which is in my template folder.
The process.php files receive and answer the data but don’t save in BDD the DATA. But if i call the process.php file manually it’s ok.
SO i think it’s AJAX which don’t do what i want. But i don’t see how to do that.

My form is like that :

<div  id="form_contact">
<form action="<?php echo get_bloginfo('stylesheet_directory')?>/process.php" 
id="contact" method="POST">

<div id="msgok" style="display:none" >
<div class="alert alert-danger"><button class="close" type="button" data-
dismiss="alert" aria-hidden="true">×</button><strong>Message envoyé</strong></div>
</div>
    <p>
        <label for="nom" class="nom">Nom</label>
        <br /><input id="nom" name="nom" type="text">
        <span id="msg_nom"></span>
    </p>
    <p>
        <label for="sujet" class="sujet">Sujet</label>
        <br /><input id="sujet" name="sujet" type="text">
        <span id="msg_sujet"></span>
    </p>
    <p>
        <label for="email">Email</label>
        <br /><input id="email" name="email" type="email">
        <span id="msg_email"></span>
    </p>
    <p>
        <label for="message">Message</label>
        <br /><textarea id="message" name="message" rows="10" cols="80">
</textarea>
       <span id="msg_message"></span>
    </p>
    <p>
        <input type="submit" value="Envoyer" />
    </p>
</form>
<span id="msg_all"></span> 
</div><!-- end of #form_contact -->`

My php file like that :

<?php ////test save bdd ////

  if ($_SERVER["REQUEST_METHOD"] == "POST") {
require_once($_SERVER['DOCUMENT_ROOT'].'/site3/wp-load.php');
require_once ($_SERVER['DOCUMENT_ROOT'].'/site3/wp-config.php');
    global $wpdb;
$table_name = $wpdb->prefix . "contact_perso";
$wpdb->insert($table_name , array('id' => "" ,'nom' => "123" ,'sujet' => 
"456",'email' => "0", 'message' => "123", 'id_auteur' => "123") );
  }


    ////test save bdd ////  



// CONDITIONS NOM
if ( (isset($_POST["nom"])) && (strlen(trim($_POST["nom"])) > 0) ) {
    $nom = stripslashes(strip_tags($_POST["nom"]));
} else {
    echo "Merci d'écrire un nom <br />";
    $nom = "";
}

// CONDITIONS SUJET
if ( (isset($_POST["sujet"])) && (strlen(trim($_POST["sujet"])) > 0) ) {
    $sujet = stripslashes(strip_tags($_POST["sujet"]));
} else {
    echo "Merci d'écrire un sujet <br />";
    $sujet = "";
}

// CONDITIONS EMAIL
if ( (isset($_POST["email"])) && (strlen(trim($_POST["email"])) > 0) && 
(filter_var($_POST["email"], FILTER_VALIDATE_EMAIL)) ) {
    $email = stripslashes(strip_tags($_POST["email"]));
} elseif (empty($_POST["email"])) {
    echo "Merci d'écrire une adresse email <br />";
    $email = "";
} else {
    echo "Email invalide :(<br />";
    $email = "";
}

// CONDITIONS MESSAGE
if ( (isset($_POST["message"])) && (strlen(trim($_POST["message"])) > 0) ) {
    $message = stripslashes(strip_tags($_POST["message"]));
} else {
    echo "Merci d'écrire un message<br />";
    $message = "";
}

// Les messages d'erreurs ci-dessus s'afficheront si Javascript est désactivé

// PREPARATION DES DONNEES
$ip           = $_SERVER["REMOTE_ADDR"];
$hostname     = gethostbyaddr($_SERVER["REMOTE_ADDR"]);
$destinataire = "monadresse@example.com";
$objet        = "[Site Web] " . $sujet;
$contenu      = "Nom de l'expéditeur : " . $nom . "rn";
$contenu     .= $message . "rnn";
$contenu     .= "Adresse IP de l'expéditeur : " . $ip . "rn";
$contenu     .= "DLSAM : " . $hostname;

$headers  = "CC: " . $email . " rn"; // ici l'expediteur du mail
$headers .= "Content-Type: text/plain; charset="ISO-8859-1"; 
DelSp="Yes"; format=flowed /r/n";
$headers .= "Content-Disposition: inline rn";
$headers .= "Content-Transfer-Encoding: 7bit rn";
$headers .= "MIME-Version: 1.0";

// SI LES CHAMPS SONT MAL REMPLIS
if ( (empty($nom)) && (empty($sujet)) && (empty($email)) && 
(!filter_var($email, FILTER_VALIDATE_EMAIL)) && (empty($message)) ) {
    echo 'echec :( <br /><a href="contact.html">Retour au formulaire</a>';
} else {
    // ENCAPSULATION DES DONNEES ok


    mail($destinataire, $objet, utf8_decode($contenu), $headers);
    echo 'Formulaire envoyé';
}

// Les messages d'erreurs ci-dessus s'afficheront si Javascript est 
désactivé
?>`

and JS script as :

<script>
$(function(){
    $("#contact").submit(function(event){
        var nom        = $("#nom").val();
        var sujet      = $("#sujet").val();
        var email      = $("#email").val();
        var message    = $("#message").val();
        var dataString = nom + sujet + email + message;
        var msg_all    = "Merci de remplir tous les champs";
        var msg_alert  = "Merci de remplir ce champs";

        if (dataString  == "") {
            $("#msg_all").html(msg_all);
        } else if (nom == "") {
            $("#msg_nom").html(msg_alert);
        } else if (sujet == "") {
            $("#msg_sujet").html(msg_alert);
        } else if (email == "") {
            $("#msg_email").html(msg_alert);
        } else if (message == "") {
            $("#msg_message").html(msg_alert);
        } else {
            $.ajax({
                type : "POST",
                url: $(this).attr("action"),
                data: $(this).serialize(),
                success : function() {
                document.getElementById("msgok").style.display = "";
                    setTimeout(function() {
                        document.getElementById("contact").style.display = 
"none";
                    }, 3000);

                },
                error: function() {
                    $("#contact").html("<p>Erreur d'appel, le formulaire ne 
peut pas fonctionner</p>");
                }
            });
        }

        return false;
    });
});
</script>`

Read more here: wordpress form ajax + save form in bdd


Solution:

If you know the solution of this issue, please leave us a reply in Comment section, to update the question.


Wordpress related questions and answers: