In this blog post, you’ll discover the process of crafting a Responsive Registration Form using HTML & CSS. Even if you’re a novice with only basic understanding of HTML and CSS, you’ll find it accessible to create this form.
A Registration Form serves as a crucial component of websites or applications, requiring users to input various details such as their name, email, address, and more.
Take a glimpse at the preview of the Registration Form you’ll be mastering through this blog. You’ll notice essential input fields essential for every Registration, along with radio buttons for selection and a submit button.
HTML:
HTML is used to create the structure and layout of the registration form. It allows web developers to define the various elements of the form, such as input fields, labels, buttons, and dropdown lists. HTML provides the basic building blocks for creating interactive web forms that users can fill out and submit. Let’s break down the structure:
Container Section: The
<section>
tag with the class “container” is used to create a section for the registration form. This helps in organizing and styling the form elements.Header: The
<header>
tag is used to define the heading of the registration form, which is “REGISTRATION FORM” in this case.Form: The
<form>
tag is used to create a form that collects user input. It has an action attribute set to “reg.php”, which specifies the URL where the form data will be submitted for processing. The method attribute is set to “POST”, indicating that form data will be sent to the server using the HTTP POST method.Input Fields: Various
<input>
tags are used to create input fields for collecting information such as full name, email address, phone number, birth date, address, city, region, and postal code. Each input field has attributes like type (text, number, date), name (to identify the input field when the form is submitted), placeholder (a hint to the user about what to enter), and required (to make the input field mandatory).Radio Buttons: The
<input>
tags with type “radio” are used to create radio buttons for selecting the gender. They are grouped together using the same name attribute, allowing the user to select only one option.Select Box: The
<select>
tag is used to create a dropdown list for selecting the country. It contains multiple<option>
tags, each representing a country. Users can select one country from the list.Submit Button: The
<button>
tag is used to create a submit button. When clicked, it submits the form data to the server for processing.
Here, I have created a file name index.html for this code. But you can name this file anything you want.
WPHotShot | Registration Form in HTML & CSS
REGISTRATION FORM
CSS:
The provided CSS code begins with an import statement fetching the Google font “Poppins” in various weights for styling text throughout the document. The universal selector (*) is then used to reset margins, paddings, and box-sizing for all elements and to set the default font family to “Poppins”.
The body is styled to ensure a minimum height of the viewport and centered alignment of its content with a gradient background. The “.container” class defines styling for a central container element holding the form, featuring padding, background color, border radius, and a box shadow for a card-like appearance.
Various selectors within “.container” handle the styling of the form’s header, input fields, columns, gender selection, and address section. Finally, media queries are employed to ensure responsive design, adjusting layout and spacing for screens narrower than 500 pixels. Additionally, a hover effect is applied to the submit button for enhanced interactivity.
Here, I have created a file name style.css for this code as you can see in the HTML code. But you can name this file anything you want.
(If you are changing the file name, make sure to change it in the index.html file as well.)
/* Import Google font - Poppins */
@import url("https://fonts.googleapis.com/css2?family=Poppins:wght@200;300;400;500;600;700&display=swap");
* {
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: "Poppins", sans-serif;
}
body {
min-height: 100vh;
display: flex;
align-items: center;
justify-content: center;
padding: 20px;
background: linear-gradient(to right, #2832d4, #00A5B2, #7209D4);
}
.container {
position: relative;
max-width: 700px;
width: 100%;
background: #fff;
padding: 25px;
border-radius: 8px;
box-shadow: 0 0 15px rgba(0, 0, 0, 0.1);
}
.container header {
font-size: 1.5rem;
color: #212121;
font-weight: 700;
text-align: center;
}
.container .form {
margin-top: 30px;
}
.form .input-box {
width: 100%;
margin-top: 20px;
}
.input-box label {
color: #212121;
font-weight: 500;
}
.form :where(.input-box input, .select-box) {
position: relative;
height: 50px;
width: 100%;
outline: none;
font-size: 1rem;
color: #919191;
margin-top: 8px;
border: 1px solid #ddd;
border-radius: 6px;
padding: 0 15px;
}
.input-box input:focus {
box-shadow: 0 1px 0 rgba(0, 0, 0, 0.1);
}
.form .column {
display: flex;
column-gap: 15px;
}
.form .gender-box {
margin-top: 20px;
}
.gender-box h3 {
color: #212121;
font-size: 1rem;
font-weight: 400;
margin-bottom: 8px;
}
.form :where(.gender-option, .gender) {
display: flex;
align-items: center;
column-gap: 50px;
flex-wrap: wrap;
}
.form .gender {
column-gap: 5px;
}
.gender input {
accent-color: #2832d4;
}
.form :where(.gender input, .gender label) {
cursor: pointer;
}
.gender label {
color: #707070;
}
.address :where(input, .select-box) {
margin-top: 15px;
}
.select-box select {
height: 100%;
width: 100%;
outline: none;
border: none;
color: #707070;
font-size: 1rem;
}
.form button {
height: 55px;
width: 100%;
color: #fff;
font-size: 1rem;
font-weight: 500;
margin-top: 30px;
border: none;
cursor: pointer;
transition: all 0.2s ease;
background: #2832d4;
}
.form button:hover {
background: #00A5B2;
}
/*Responsive*/
@media screen and (max-width: 500px) {
.form .column {
flex-wrap: wrap;
}
.form :where(.gender-option, .gender) {
row-gap: 15px;
}
}
Overall, this CSS code provides a visually appealing and responsive layout for the registration form, ensuring a consistent user experience across different devices and screen sizes.
PHP:
The provided PHP code establishes a connection to a MySQL database and handles the backend processing of a registration form. It begins by defining the database connection details and creating a connection object using the mysqli class. Following this, it checks if the form has been submitted via the POST method. Upon form submission, the code prepares an SQL INSERT statement to add the user’s input data into the “users” table of the specified database.
To prevent SQL injection attacks, user inputs are sanitized using real_escape_string() and validated using filter_var() where necessary, such as for email validation. Once the SQL statement is prepared and the parameters are bound, the code executes the statement. If the execution is successful, it echoes a message indicating that a new record has been created. In case of any errors during execution, it displays an error message. Finally, the code closes the prepared statement and the database connection.
(If you are changing the file name, make sure to change it in the index.html file as well.)
Overall, this PHP script facilitates the secure handling and processing of user input data from a registration form, ensuring that the data is properly sanitized, validated, and stored in the database for further use.
connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Check if form is submitted
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Prepare and bind SQL statement
$stmt = $conn->prepare("INSERT INTO users (full_name, email, phone_number, birth_date, gender, street_address1, street_address2, country, city, region, postal_code) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
$stmt->bind_param("ssssssssssi", $full_name, $email, $phone_number, $birth_date, $gender, $street_address1, $street_address2, $country, $city, $region, $postal_code);
// Escape and validate user inputs
$full_name = htmlspecialchars($conn->real_escape_string($_POST['full_name']));
$email = filter_var($conn->real_escape_string($_POST['email']), FILTER_VALIDATE_EMAIL);
$phone_number = $conn->real_escape_string($_POST['phone_number']);
$birth_date = $conn->real_escape_string($_POST['birth_date']);
$gender = $conn->real_escape_string($_POST['gender']);
$street_address1 = htmlspecialchars($conn->real_escape_string($_POST['street_address1']));
$street_address2 = htmlspecialchars($conn->real_escape_string($_POST['street_address2']));
$country = $conn->real_escape_string($_POST['country']);
$city = htmlspecialchars($conn->real_escape_string($_POST['city']));
$region = htmlspecialchars($conn->real_escape_string($_POST['region']));
$postal_code = $conn->real_escape_string($_POST['postal_code']);
// Execute SQL statement
if ($stmt->execute()) {
echo "New record created successfully";
} else {
echo "Error: " . $stmt->error;
}
// Close statement
$stmt->close();
}
// Close connection
$conn->close();
?>
Conclusion:
In conclusion, the collective article provides comprehensive insights into creating a responsive registration form, incorporating HTML, CSS, and PHP. Through a step-by-step breakdown, it elucidates the importance of each component in facilitating a seamless user experience. HTML establishes the structural foundation, CSS adds stylistic elements for enhanced visual appeal and responsiveness, while PHP ensures the form’s functionality and data handling on the server-side. By leveraging modern techniques such as flexbox, media queries, and Google Fonts integration, the article equips developers with the tools to craft registration forms that adapt fluidly across various devices and screen sizes. With a clear understanding of the code snippets and their application, readers are empowered to implement responsive design principles effectively in their web development projects.
One Response
Great tutorial, thanks.