How to create self-processing forms in PHP PDO

 

self-processing forms in PHP PDO



In this tutorial, we are going to learn about how to create self-processing forms in PHP PDO.Let's see below.

Self-Processing Form in PHP


The self-processing form in PHP PDO is useful in the tutorial and this creates forms that submit data to a database, send an email like contact forms or perform other actions without the need for the user to navigate to a different page.
 

why we are using self-processing form?


Self-processing forms are powerful forms for building dynamic websites and applications. we will explore how to create self-processing forms in PHP using PDO (PHP Data Objects).


PHP PDO Connection


First, we need to set up a database connection using PDO. This connection can be done by creating a way for passing on necessary information. such as the database type, hostname, and credentials. once the database connection is established. using HTML to create form fields.


try {
        $pdo = new PDO("mysql:host=localhost;dbname=webontools", "root", "");
        // set the PDO error mode to exception
        $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
    catch(PDOException $e) {
        echo "Error: " . $e->getMessage();
    }

Create a HTML Form


HTML Form is used to process the form data and update the database easily. Here I am using the bootstrap framework to design the HTML Form Fields.

self-processing forms in PHP PDO

ok, we will create a simple form that allows users to submit their name email address, message, and phone number. Here I created three fields name, email address, and password.

PHP Form



<div class="container py-5">
        <div class="col-md-6 mx-auto"> 
        <form method="post" class="form-control p-4" action="submit.php">
        <div class="row g-3 align-items-center">
        <h1 class="py-3 text-center">Self Processing forms in PHP</h1>
        <div class="mb-3">
        <label class="form-label" for="name">Name:</label>
        <input class="form-control" type="text" id="name" name="name"><br>
       
        </div>
        <div class="mb-3">
        <label class="form-label" for="email">Email:</label>
        <input class="form-control" type="email" id="email" name="email"><br>
        
        </div>
        <div class="mb-3">
        <label class="form-label" for="password">Password:</label>
        <input class="form-control" type="password" id="password" name="password"><br>
        
        </div>
        <input class="btn btn-success" type="submit" value="Submit">
        </div>
</form>
</div>
        </div>

PHP Post Method


The HTML form uses the "post" method to submit the data to the server-side script easily. Here I name the update to the database form named "sumbit.php"
 

submit.php



if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $name = $_POST["name"];
    $email = $_POST["email"];
    $password = $_POST["password"];

    
    try {
        $pdo = new PDO("mysql:host=localhost;dbname=webontools", "root", "");
        // set the PDO error mode to exception
        $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        
        $query = "INSERT INTO users (name, email, password) VALUES (:name, :email, :password)";
        $stmt = $pdo->prepare($query);
        $stmt->bindParam(':name', $name);
        $stmt->bindParam(':email', $email);
        $stmt->bindParam(':password', $password);
        $stmt->execute();
        //echo "data instered successfully";
        header("Location: selfform.php");
    }
    catch(PDOException $e) {
        echo "Error: " . $e->getMessage();
    }
}  
  

In the like above already, we created the connection to the database using PDO and then used form data to send the variable by "$_POST" array to access the values entered by the user.


create table users (
id int PRIMARY KEY AUTO_INCREMENT,
name varchar(30) not null,
email varchar(100)NOT null,
password varchar(100)
);

And one more thing doesn't forget to create a SQL statement to insert the data. Here I create a table named as "users" table and the using of PDO prepare() and execute() methods statement to confirm the form has been submitted.And then redirected to php Form named as "selfform.php".


$query = "INSERT INTO users (name, email, password) VALUES (:name, :email, :password)";
        $stmt = $pdo->prepare($query);
        $stmt->bindParam(':name', $name);
        $stmt->bindParam(':email', $email);
        $stmt->bindParam(':password', $password);
        $stmt->execute();
        echo "data instered successfully";


header("Location: selfform.php");

conclusion:

 
In the conclusion, I hope you will learn how to create a self-processing form in PHP using PDO. This allows users to interact easily and send data. And PHP PDOs are a powerful and efficient way to handle the user data to update the database and there is no SQL injection that prevents occurring easily.

Previous Post Next Post