Ajax Serialize Form Data in PHP

 <?php


$servername = "localhost";
$username = "root";
$password = "";
$database = "ajax-form";

//Create Connection
$conn = new mysqli($servername, $username, $password, $database);

//Check Connection
if ($conn->connect_error) {
    die("Connection Failed: " . $conn->connect_error);
}
Above is mysqli_conn.php File which is included in below Files.






Below is index.php File.
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>PHP Ajax Pagination</title>
    <link rel="stylesheet" href="css/style.css">
</head>

<body>
    <div id="main">
        <div id="header">
            <h1>PHP & Ajax Serialize Form</h1>
        </div>

        <div id="table-data">
            <!--When we use serialize() at that time <form> Tag is necessary-->
            <form id="submit_form">
                <table width="100%" cellspacing="10px">
                    <tr>
                        <td width="150px"><label>Name</label></td>
                        <td><input type="text" name="fullname" id="fullname"></td>
                    </tr>
                    <tr>
                        <td><label>Age</label></td>
                        <td><input type="number" name="age" id="age"></td>
                    </tr>
                    <tr>
                        <td><label>Gender</label></td>
                        <td>
                            <input type="radio" name="gender" value="male">Male
                            <input type="radio" name="gender" value="female">Female
                        </td>
                    </tr>
                    <tr>
                        <td><label>Country</label></td>
                        <td>
                            <select name="country">
                                <option value="ind">India</option>
                                <option value="pk">Pakistan</option>
                                <option value="ban">Bangladesh</option>
                                <option value="ne">Nepal</option>
                                <option value="sl">Srilanka</option>
                            </select>
                        </td>
                    </tr>
                    <tr>
                        <td></td>
                        <td><input type="button" name="submit" id="submit" class="btn btn-info" value="Save"></td>
                    </tr>
                </table>
            </form>
            <div id="response"></div>
        </div>
    </div>
    <script type="text/javascript" src="js/jquery-3.7.1.min.js"></script>
    <script>
        $(document).ready(function() {
            $("#submit").click(function() {
                var name = $("#fullname").val();
                var age = $("#age").val();

                if (name == "" || age == "" || !$("input:radio[name=gender]").is(":checked")) {
                    $("#response").fadeIn();
                    $("#response").removeClass("success-msg").addClass("error-msg").html("All fields are required.");

                    setTimeout(function() {
                        $("#response").fadeOut("slow");
                    }, 4000);
                } else {
                    // $("#response").html($("#submit_form").serialize());

                    $.ajax({
                        url: "save-form.php",
                        type: "POST",
                        data: $("#submit_form").serialize(),
                        beforesend: function() {
                            $("#response").fadeIn();
                            $("#response").removeClass("success-msg error-msg").addClass("process-msg").html("Loading response...");
                        },
                        success: function(data) {
                            $("#submit_form").trigger("reset");
                            $("#response").fadeIn();
                            $("#response").removeClass("error-msg").addClass("success-msg").html(data);

                            setTimeout(function() {
                                $("#response").fadeOut("slow");
                            }, 4000);
                        }
                    });
                }
            });
        });
    </script>
</body>

</html>







Below is save-form.php File.
<?php

include("mysqli_conn.php");

$name = $_POST["fullname"] ?? null;
$age = $_POST["age"] ?? null;
$gender = $_POST["gender"] ?? null;
$country = $_POST["country"] ?? null;

$sql = "INSERT INTO students(name, age, gender, country) VALUES('{$name}',$age,'{$gender}','{$country}')";

if ($conn->query($sql) == TRUE) {
    echo "Hello {$name}, your record is saved.";
} else {
    echo "Can't save form data.";
}








Below is css/style.css File.
body {
    font-family: arial;
    background: #b2bec3;
    padding: 0;
    margin: 0;
}

h1 {
    text-align: center;
    font-size: 40px;
    margin: 0;
}

#main {
    width: 700px;
    margin: 20px auto;
    background: #fff;
    font-size: 18px;
}

#header {
    background: #6c5ce7;
    color: #fff;
    padding: 15px;
}

#table-data {
    padding: 15px;
    min-height: 400px;
}

table tr td:first-child {
    text-align: right;
    font-weight: bold;
}

input[type="text"],
input[type="number"],
select {
    width: 200px;
    height: 25px;
    font-size: 17px;
    padding: 3px;
}

input[type="number"] {
    width: 60px;
}

input[type="button"] {
    background: #6c5ce7;
    color: #fff;
    border: 0;
    border-radius: 3px;
    padding: 5px 10px;
    outline: 0;
    cursor: pointer;
}

#response {
    width: 70%;
    margin: 15px auto;
    padding: 15px;
    border-radius: 5px;
}

.error-msg {
    background: #f2dedf;
    color: #9c4150;
    border: 1px solid #e7ced1;
}

.success-msg {
    background: #e0efda;
    color: #407a4a;
    border: 1px solid #c6dfb2;
}

.process-msg {
    background: #d9edf6;
    color: #377084;
    border: 1px solid #c8dce5;
}






Comments

Popular posts from this blog

Logical_Operators

SubQuery with EXISTS and NOT EXISTS in PHP

Get Functions