Header Ads

Header ADS

Php Files & MySql Database Setup

 MySQL Database Setup

CREATE DATABASE formdb;


USE formdb;


CREATE TABLE contacts (

    id INT AUTO_INCREMENT PRIMARY KEY,

    firstname VARCHAR(100),

    lastname VARCHAR(100),

    email VARCHAR(100),

    phone VARCHAR(30),

    message TEXT,

    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP

);

index.html (html form code)

<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8" />

    <meta name="viewport" content="width=device-width, initial-scale=1.0" />

    <title>Contact Form</title>


    <style>

        *{

            margin:0;

            padding:0;

            box-sizing:border-box;

            font-family:Arial, sans-serif;

        }


        body{

            height:100vh;

            display:flex;

            justify-content:center;

            align-items:center;

            background:#f4f4f4;

        }


        .container{

            width:100%;

            max-width:420px;

            background:#ffffff;

            padding:30px;

            border-radius:10px;

            box-shadow:0 0 10px rgba(0,0,0,0.1);

        }


        h2{

            text-align:center;

            margin-bottom:20px;

            color:#333;

        }


        .form-group{

            margin-bottom:15px;

        }


        label{

            display:block;

            margin-bottom:6px;

            font-weight:bold;

            color:#444;

        }


        input,

        textarea{

            width:100%;

            padding:12px;

            border:1px solid #ccc;

            border-radius:5px;

            font-size:15px;

            outline:none;

        }


        textarea{

            resize:none;

            height:100px;

        }


        input:focus,

        textarea:focus{

            border-color:#007bff;

        }


        button{

            width:100%;

            padding:12px;

            border:none;

            background:#007bff;

            color:white;

            font-size:16px;

            font-weight:bold;

            border-radius:5px;

            cursor:pointer;

            transition:0.3s;

        }


        button:hover{

            background:#0056b3;

        }

    </style>

</head>


<body>


    <div class="container">


        <h2>Contact Form</h2>


        <form action="insert.php" method="POST">


            <div class="form-group">

                <label>FirstName</label>

                <input type="text" name="firstname" required />

            </div>


            <div class="form-group">

                <label>LastName</label>

                <input type="text" name="lastname" required />

            </div>


            <div class="form-group">

                <label>Email</label>

                <input type="email" name="email" required />

            </div>


            <div class="form-group">

                <label>Phone</label>

                <input type="tel" name="phone" required />

            </div>


            <div class="form-group">

                <label>Message</label>

                <textarea name="message" required></textarea>

            </div>


            <button type="submit">Submit</button>


        </form>


    </div>


</body>

</html>



db.php

<?php


$servername = "localhost";

$username = "root";

$password = "";

$dbname = "formdb";


// Create connection

$conn = new mysqli($servername, $username, $password, $dbname);


// Check connection

if ($conn->connect_error) {

    die("Connection failed: " . $conn->connect_error);

}


?>


insert.php 

<?php


include 'db.php';


if ($_SERVER["REQUEST_METHOD"] == "POST") {


    $firstname = $_POST['firstname'];

    $lastname  = $_POST['lastname'];

    $email     = $_POST['email'];

    $phone     = $_POST['phone'];

    $message   = $_POST['message'];


    $stmt = $conn->prepare(

        "INSERT INTO contacts 

        (firstname, lastname, email, phone, message) 

        VALUES (?, ?, ?, ?, ?)"

    );


    $stmt->bind_param(

        "sssss",

        $firstname,

        $lastname,

        $email,

        $phone,

        $message

    );


    if ($stmt->execute()) {


        echo "

        <script>

            alert('Submitted Successfully');

            window.location.href='index.html';

        </script>

        ";


    } else {


        echo "

        <script>

            alert('Error: Something went wrong');

        </script>

        ";

    }


    $stmt->close();

    $conn->close();

}


?>


No comments

Powered by Blogger.