How to Process a Form using PHP - onlyxcodes

Wednesday 3 July 2024

How to Process a Form using PHP

Hello, We will learn how to use PHP to process a form and submit the form data to the MySQL database in this tutorial.


PHP form handling is an essential ability for any web developer. You will be guided through all the necessary procedures to construct, submit, and receive a PHP form by following this guide. The fundamentals of creating HTML forms, processing PHP forms using the POST method, validating data, and importing form data into a MySQL database will all be covered in this tutorial.


how to process a form using php

Table Content

1. Database and Table

2. dbconfig.php

3. index.php

4. PHP Script


1. Database and Table

In PHPMyAdmin, create a database with any name you choose, then copy and paste the following SQL code to create the User table. The database I used for this creation is called "process_form_db."


CREATE TABLE `tbl_user` (
  `id` int(11) NOT NULL,
  `firstname` varchar(100) NOT NULL,
  `lastname` varchar(100) NOT NULL,
  `email` varchar(255) NOT NULL,
  `password` varchar(255) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

2. dbconfig.php

I have used the PDO extension to create a MySQL database connection in this file.


<?php

$db_host="localhost"; 
$db_user="root";	
$db_password="";	
$db_name="process_form_db";	

try
{
	$db=new PDO("mysql:host={$db_host};dbname={$db_name}",$db_user,$db_password);
	$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOEXCEPTION $e)
{
	$e->getMessage();
}

?>

3. index.php

I used the Bootstrap package to create a basic HTML form in this file. The user's email address, password, and first and last names will be gathered using this form.


<form method="post" class="form-horizontal">
					
	<div class="form-group">
	<label class="col-sm-3 control-label">Firstname</label>
	<div class="col-sm-6">
	<input type="text" name="txt_fname" class="form-control" placeholder="enter firstname">
	</div>
	</div>
				
	<div class="form-group">
	<label class="col-sm-3 control-label">Lastname</label>
	<div class="col-sm-6">
	<input type="text" name="txt_lname" class="form-control" placeholder="enter firstname">
	</div>
	</div>
				
	<div class="form-group">
	<label class="col-sm-3 control-label">Email</label>
	<div class="col-sm-6">
	<input type="text" name="txt_email" class="form-control" placeholder="enter firstname">
	</div>
	</div>
				
	<div class="form-group">
	<label class="col-sm-3 control-label">Password</label>
	<div class="col-sm-6">
	<input type="password" name="txt_password" class="form-control" placeholder="enter firstname">
	</div>
	</div>
				
				
				
	<div class="form-group">
	<div class="col-sm-offset-3 col-sm-6 m-t-15">
	<input type="submit" name="btn_insert" value="Insert" class="btn btn-success btn-lg btn-block" >
	</div>
	</div>
				
</form>
			
<?php
			
if(isset($errorMsg))
{
?>
	<div class="alert alert-danger alert-dismissible">
		<strong>WRONG ! <?php echo $errorMsg; ?></strong>
		<button type="button" class="close" data-dismiss="alert">&times;</button>
	</div>
<?php
}
if(isset($insertMsg))
{
?>
	<div class="alert alert-success alert-dismissible">
		<strong>SUCCESS ! <?php echo $insertMsg; ?></strong>
		<button type="button" class="close" data-dismiss="alert">&times;</button>
	</div>
<?php
}
?>

4. PHP Script

I used the super global POST method to gather form values for this script. Forms containing a lot of data, such as those for register and log-in, file uploads, and image uploads, are frequently sent via the Post method.


The PDO code for adding new user records to the MySQL database is included in this PHP script. It follows the correct validation format, which includes requiring that password values have at least six characters and that form fields have non-blank values.


<?php

require_once "dbconfig.php";

if(isset($_POST['btn_insert']))
{
	$fname = $_POST['txt_fname'];	//get firstname value "txt_fname"
	$lname = $_POST['txt_lname'];	//get firstname value "txt_lname"
	$email = $_POST['txt_email'];	//get firstname value "txt_email"
	$password = $_POST['txt_password'];	//get firstname value "txt_password"
	
	if(empty($fname)){
		$errorMsg="Please Enter Your Firstname";
	}
	else if(empty($lname)){
		$errorMsg="Please Enter Your lastname";
	}
	else if(empty($email)){
		$errorMsg="Please enter email";
	}
	else if(!filter_var($email, FILTER_VALIDATE_EMAIL)){
		$errorMsg="Please enter a valid email address";
	}
	else if(empty($password)){
		$errorMsg="Please enter password";
	}
	else if(strlen($password) < 6 ){
		$errorMsg="Please enter password at least 6 characters";
	}

	else
	{
		try
		{
			if(!isset($errorMsg))
			{
				$insert_stmt=$db->prepare('INSERT INTO tbl_user(firstname,lastname,email,password) VALUES(:ufname,:ulname,:uemail,:upassword)'); //sql insert query					
				$insert_stmt->bindParam(':ufname',$fname); //bind insert query parameter of :ufname
				$insert_stmt->bindParam(':ulname',$lname); //bind insert query parameter of :ulname
				$insert_stmt->bindParam(':uemail',$email); //bind insert query parameter of :uemail
				$insert_stmt->bindParam(':upassword',$password); //bind insert query parameter of :upassword
				
				
				if($insert_stmt->execute())
				{
					$insertMsg="records inserted successfully in database........"; //run query success message
				}
			}
		}
		catch(PDOException $e)
		{
			echo $e->getMessage();
		}
	}
}

?>

Learn More:

What does array splice do in PHP

What is POST variable in PHP

What is Custom PHP Development

How do you append to text in PHP

How to make OTP Verification in PHP

How to store textarea value in database in PHP

How to Fetch Data from Database in JSON format in PHP

How to get the class name in PHP

How to Run PHP Code in Browser

How to Check if a String is Null in PHP

How to Pass Dropdown Selected Value to Database in PHP

How to Create Drop Down List in PHP with MySQL

No comments:

Post a Comment

Post Bottom Ad