Download!Download Point responsive WP Theme for FREE!

Build a simple PHP registration form with auto email system

build a simple php registration form

Today we will talk about a simple PHP form that can get and insert user data into the database and can send an auto email to the user’s email account regarding his registration details. We will have three parts in this tutorial. Number one is HTML form, second is PHP code and the last thing is to give a good design to our form for which we will use CSS.

So first talk about a simple HTML form,

Html Form

Html stands for hyper text markup language, the basic use of html is to design a skeleton of a page. Later we can put flash on that skeleton by using CSS.

In this tutorial we will just focus on building a form, below is the code for our simple form

[code]

<form action=’test.php’ method = ‘POST’>

Username :

<input name=’Username’ type=’text’ placeholder=’Type a Username’ />

Password :

<input name=’Password’ type=’Password’ placeholder=’Type your Password’ />

Confirm Password :

<input name=’confirmP’ type=’Password’ placeholder=’Type your Password’ />

Email Address

<input name=’Email’ type=’Text’ placeholder=’Type your email address’ />

Select Gender

<input type=’radio’ name=’gender’ value=’female’>Female</input>

<input type=’radio’ name=’gender’ value=’male’>Male
<input type=’submit’ name=’submit’ value=’Submit’>
</form>

[/code]

This html code will create a simple HTML form where you can ask a user to type a Username, Password, Email and his/her gender.

Now let’s clear few concepts right here. To build a form on HTML, you need to first use a <form> tag, notice that inside it we have two attributes method and action.

Action defines that where the user input should be sent and the method attribute tells the system that which method it should use to send data. Normally, there are two methods used. One is POST and the other one is GET.

Now let’s start styling our form, for that we will use CSS.

CSS

Now that we have created a basic HTML template for our form, it is time to start styling it. For that we will use CSS3, You can style it in what ever manner you want to.

However, I will not give much attention on styling it. We will only design few areas and then go to the back-end work where we will use PHP to collect user input and then to store it in the Database.

[css]

form p {

width: 49%;
float: left;
margin: 5px;
}

form input {
padding: 2px;
border: 2px solid #E4E1E1;
}

form input:focus {

padding: 2px;
border: 2px solid rgba(21, 82, 209, 0.63);

[/css]

PHP

Now that we have also designed our HTML template with CSS, it is the time to do the back-end work(collection user data and then storing it into database).

For that we will use PHP, All we need to do is to first get user data and then store it into database. But wait a second, we haven’t yet created a database. Actually, I have already created one and here are the details:

  • Database management system (DMS) = MySQL (You can use any other like MongoDB etc)
  • Database name is phpform
  • Username is user1
  • Password is password1
  • We have also created a table named ‘userdetails’ where we will store a user’s input which has four columns username, password, email and gender.

As I have already told you that first we will get a user’s data from PHP and then we will store it in the database, for that let’s build a simple PHP script.

[php]

//To check that all details are filled or not

if(isset($_POST[‘Username’], $_POST[‘Password’], $_POST[‘confirmP’],

$_POST[‘Email’], $_POST[‘gender’])){

//check whether password and confirm password are matched or not

if($_POST[‘Password’] == $_POST[‘confirmP’]){

//to check if email is valid or not

if(filter_var($_POST[‘Email’], FILTER_VALIDATE_EMAIL)){

try {

$usernameP = $_POST[‘Username’];
$passwordP = $_POST[‘Password’];
$confirmP = $_POST[‘confirmP’];
$emailP = $_POST[‘Email’];
$genderP = $_POST[‘gender’];

$conn = new PDO("mysql:host=localhost;dbname=phpform", ‘root’, ”);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

// prepare sql and bind parameters
$stmt = $conn->prepare("INSERT INTO `userdetails`(`username`, `password`, `email`, `gender`) VALUES (
:username, :password, :email, :gender)");
$stmt->bindParam(‘:username’, $usernameP);
$stmt->bindParam(‘:password’, $passwordP);
$stmt->bindParam(‘:email’, $emailP);
$stmt->bindParam(‘:gender’, $genderP);
if($stmt->execute()){
$subject = "Write a subject";

$body = "Your Message";

$headers = "From: Articles Teller<no-reply@articlesteller.com>";

mail($emailP,$subject, $body, $headers);
}

}
catch(PDOException $e)
{
echo "Error: " . $e->getMessage();
}
} else {
echo "Invalid Email";
}
} else {
echo "Password does not match";
}
} else {
echo "Incomplete details";
} [/php]

Now we have created a simple php script which will get and store all the user data into the database.

You can also add many more input fields like “Your address” for that you will need to add some new code to your PHP script.

For more info about PHP forms, please read w3schools php forms tutorials.

Please subscribe for more php tutorials and let me know if you are having any difficulties.

One Comment

Add a Comment

Your email address will not be published. Required fields are marked *