Insert users data in MySQL database with php

I have a form inserted in my site to allow users to contact me.

Here is my index.php:

 <!-- Form Start-->
                <div class="contact_from">
                    <form action="hel.php?#contact_from" method="post">
                        <!-- Message Input Area Start -->
                        <div class="contact_input_area">
                            <div class="row">
                                <!-- Single Input Area Start -->
                                <div class="col-md-12">
                                    <div class="form-group">
                                        <input type="text" class="form-control" name="nom" id="name" placeholder="Nom" required>
                                    </div>
                                </div>
                                <!-- Single Input Area Start -->
                                <div class="col-md-12">
                                    <div class="form-group">
                                        <input type="email" class="form-control" name="email" id="email" placeholder="E-mail" required>
                                    </div>
                                </div>
                                <!-- Single Input Area Start -->
                                <div class="col-12">
                                    <div class="form-group">
                                        <textarea name="message" class="form-control" id="message" cols="30" rows="4" placeholder="Entrez votre message *" required></textarea>
                                    </div>
                                </div>
                                <!-- Single Input Area Start -->
                                <div class="col-12">
                                    <button type="submit" class="btn submit-btn">Envoyer<button>

                                </div>
                            </div>
                        </div>
                        <!-- Message Input Area End -->
                    </form>

And here is my php file that processes my form:

<?php
$nom  = $_POST['nom'];
$email = $_POST['email'];
$message = $_POST['message'];


// Create connection
$conn = mysqli_connect($localhost, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}

$sql = "INSERT INTO contact_site (nom, email, message)
VALUES ('$nom', '$email', '$message')";

if (mysqli_query($conn, $sql)) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}

mysqli_close($conn);
?>

Everything works perfectly except that when I confirm my form, I am redirected to another page.

So here is my problem: I would like that when the user sends the vormulaire, a small confirmation message is sent to him without redirecting it to a new web page. How can I do this please?

1 answer

  • answered 2018-03-11 20:08 BambiOurLord

    You may show user a confirmation box and not redirect him to another page if he clicks no by doing the following:

    You should add an onsubmit to your button like so

      <button type="submit" class="btn submit-btn" onsubmit="return confirmBox();"> Envoyer <button>
    

    return - added to onsubmit attribute it will make it so that the form will only be submitted if true is returned, else if false is returned then the form will not be submitted.

    then you need to add JavaScript

    <script>
        function confirmBox(){
            if ( confirm( 'Are you sure you want to submit details?' ) ){
                return true;
            } else if {
                return false;
            }
        }
    </script>
    

    Upon user clicking the submit button he will be shown a pop-up with Yes or No option, if the user clicks Yes, return true and submit the form else if User has clicked No, then return false and do not submit the form.

    You may add JavaScript directly in this file with tags or create a separate .js file.