Now we are going to create a simple user registration form with all input fields:
- text input fields
- password
- radio buttons
- checkboxes
- select options
- file upload
We need table to store our data . First create a database in your phpmyadmin. Then run below code to create table inthat database.
CREATE TABLE `registerform` (
`id` int(12) NOT NULL,
`name` varchar(200) DEFAULT NULL,
`email` varchar(200) DEFAULT NULL,
`password` varchar(200) DEFAULT NULL,
`gender` varchar(200) DEFAULT NULL,
`hobbies` varchar(200) DEFAULT NULL,
`country` varchar(200) DEFAULT NULL,
`resume` varchar(200) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
BELOW CODE CONTAINS “DB CONNECTIONS” , “SQL DATA INSERT” AND HTML “FORM CODE” IN SINGLE FORM ONLY
SAVE WITH (registerform.php)
<?php
$dbhost = "localhost";
$dbuser = "root";
$dbpass = "";
$db = "malleshtekumatla";
$conn = new mysqli($dbhost, $dbuser, $dbpass,$db);
//print_r($_POST);exit;
if( $_POST ){
extract( $_POST );
$file =$_FILES['resume']['name'];
$file_loc = $_FILES['resume']['tmp_name'];
$folder="uploads/";
move_uploaded_file($file_loc,$folder.$file);
$hobbiesimpo = implode(",",$hobbies);
// Check connection
if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } $sql="INSERT INTO registerform(name,email,password,gender,hobbies,country,resume)
VALUES('$name','$email','$password','$gender','$hobbiesimpo','$country','$file')"; if ($conn->query($sql) === TRUE) { echo "
<h1 style="text-align: center;">New record created successfully</h1>
"; } else { echo "Error: " . $sql . "<br />
" . $conn->error; } $conn->close(); } ?>
<html>
<head>
<title>MY HTML FORM</title>
<style>
td {
color: yellow;
font-size: 15px;
border: 1px solid red;
}
input,
textarea,
select {
font-size: 20px;
padding: 10px;
border-radius: 9px;
}
</style>
</head>
<body bgcolor="yellow">
<form name="form" method="post" enctype="multipart/form-data">
<table width="840" align="center" bgcolor="green" cellpadding="15" border="5px" color="red">
<tr>
<td colspan="2" align="center"><h2>REGISTRAION FORM</h2></td>
</tr>
<tr>
<td>USER NAME :</td>
<td><input type="text" name="name" placeholder="Please Enter username" /></td>
</tr>
<tr>
<td>EMAIL :</td>
<td><input type="email" name="email" placeholder="Please Enter Email" /></td>
</tr>
<tr>
<td>PASSWORD :</td>
<td><input type="password" name="password" placeholder="Please Enter password" /></td>
</tr>
<tr>
<td>GENDER :</td>
<td>
<input type="radio" name="gender" value="male" />MALE <br />
<input type="radio" name="gender" value="female" />FE MALE
</td>
</tr>
<tr>
<td>HOBBIES :</td>
<td><input type="checkbox" name="hobbies[]" value="crircket" />Cricket <input type="checkbox" name="hobbies[]" value="football" />FootBall <input type="checkbox" name="hobbies[]" value="chess" />Chess</td>
</tr>
<tr>
<td>SELECT COUNTRY :</td>
<td>
<select name="country">
<option value="India"> India</option>
<option value="India"> China</option>
<option value="Australia"> Australia</option>
<option value="Newzeland"> Newzeland</option>
</select>
</td>
</tr>
<tr>
<td>UPLOAD YOUR RESUME :</td>
<td><input type="file" name="resume" /></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" /></td>
</tr>
</table>
</form>
</body>
</html>
NOTE: For file upload you need to create a “uploads” folder in your root folder.
How to execute file in local system ?
http://localhost/MALLESH/registerform.php
In my case iam using “xampp” the file path like below
C:\xampp\htdocs\MALLESH\ http://localhost/MALLESH/registerform.php
Above path “MALLESH” is my root folder name you can give any name
Model 2 form when we have LIVE DATABASE DETAILS
TABLE CODE FOR CREATION
CREATE TABLE `contact` (
`id` int(12) NOT NULL,
`name` varchar(200) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`lastname` varchar(200) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`email` varchar(200) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`phonenumber` varchar(200) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`city` varchar(200) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`message` varchar(200) COLLATE utf8mb4_unicode_ci DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
SAVE WITH (registerform2.php)
<?php
$dbhost = "localhost";
$dbuser = "u657728665_balimistudios";
$dbpass = "Balimistudios@123";
$db = "u657728665_balimistudios";
$conn = new mysqli($dbhost, $dbuser, $dbpass,$db);
if( $_POST )
{
//print_r($_POST);exit;
//getting the post values
$name=$_POST['name'];
$lastname=$_POST['lastname'];
$email=$_POST['email'];
$phonenumber=$_POST['phonenumber'];
$city=$_POST['city'];
$message=$_POST['message'];
// Query for data insertion
$query=mysqli_query($conn, "insert into contact(name,lastname,email,phonenumber,city,message)
value('$name','$lastname', '$email', '$phonenumber', '$city','$message' )");
if ($query) {
$to = "ENTER YOUR ADMIN EMAIL WHOM YOU WANT TO SEND MAILS"; // this is your Email address
$from = $_POST['email']; // this is the sender's Email address
$first_name = $_POST['name'];
$last_name = $_POST['lastname'];
$subject = "Form submission";
$subject2 = "Copy of your form submission";
$message = $first_name . " " . $last_name . " wrote the following:" . "\n\n" . $_POST['message'];
$message2 = "Here is a copy of your message " . $first_name . "\n\n" . $_POST['message'];
$headers = "From:" . $from;
$headers2 = "From:" . $to;
mail($to,$subject,$message,$headers);
mail($from,$subject2,$message2,$headers2); // sends a copy of the message to the sender
echo "Mail Sent. Thank you " . $first_name . ", we will contact you shortly.";
// You can also use header('Location: thank_you.php'); to redirect to another page.
// You cannot use header and echo together. It's one or the other.
}
echo "<h1 style='text-align: center;'>New record created successfully</h1>";
echo "<script type='text/javascript'> document.location ='httpsttttt://somesite.com/contact.html'; </script>";
}
else
{
echo "<script>alert('Something Went Wrong. Please try again');</script>";
}
?>
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
body {font-family: Arial, Helvetica, sans-serif;}
* {box-sizing: border-box;}
input[type=text], select, textarea {
width: 100%;
padding: 12px;
border: 1px solid #ccc;
border-radius: 4px;
box-sizing: border-box;
margin-top: 6px;
margin-bottom: 16px;
resize: vertical;
}
input[type=submit] {
background-color: #04AA6D;
color: white;
padding: 12px 20px;
border: none;
border-radius: 4px;
cursor: pointer;
}
input[type=submit]:hover {
background-color: #45a049;
}
.container {
border-radius: 5px;
background-color: #f2f2f2;
padding: 20px;
}
</style>
</head>
<body>
<h3>Contact Form</h3>
<div class="container">
<form name="form" method="post" enctype="multipart/form-data">
<label for="fname">First Name</label>
<input type="text" id="fname" name="name" placeholder="Your name..">
<label for="lname">Last Name</label>
<input type="text" id="lname" name="lastname" placeholder="Your last name..">
<label for="fname">Email</label>
<input type="email" id="lname" name="email" placeholder="Your Email.."><br>
<br>
<br>
<label for="fname">Phone Number</label>
<input type="text" id="lname" name="phonenumber" placeholder="Your Phonenumber..">
<br>
<br>
<label for="fname">City</label>
<input type="text" id="lname" name="city" placeholder="Your City.."><br>
<br>
<br>
<label for="subject">Subject</label>
<textarea id="subject" name="message" placeholder="Write something.." style="height:200px"></textarea>
<input type="submit" value="Submit">
</form>
</div>
<a href="https://wa.me/9989XXXXXXX" target="_blank">
<img src="https://bas.com/img/whatsapp-logo-icone.png" width="50px"></a>
</body>
</html>