start learning
Image 1

PHP Forms examples

PHP forms are an essential component of web development, allowing website visitors to input and submit data to be processed by a server-side script written in the PHP programming language. Forms provide an interface for users to interact with a website, providing a convenient way to collect information, input data, and perform various actions.

Here are some basic examples of PHP forms that can help you get started :
Example 1: Simple Contact Form

This form collects a visitor's name, email address, and message and sends an email to a specified email address.

php forms

   <form method="post" action="send-mail.php">
  <label for="name">Name:</label>
  <input type="text" name="name" id="name" required>
  <label for="email">Email:</label>
  <input type="email" name="email" id="email" required>
  <label for="message">Message:</label>
  <textarea name="message" id="message" required> </textarea>
  <button type="submit">Send</button>
  
     

This HTML code creates a simple contact form that includes three input fields - "Name", "Email", and "Message" - and a submit button. The form's method attribute is set to "post", which means the form data will be sent to the server as an HTTP POST request when the user submits the form. The action attribute specifies the URL of the PHP script that will handle the form submission.

The input fields all have a required attribute, which means the user must fill in these fields before submitting the form. The id attribute of each input field is used to identify the input in the PHP code that processes the form data.

Adding some css code to style the form, This CSS code will give the form a modern look with a border, rounded corners, and padding. You can customize the colors and font sizes to your preference using the
liveeditor :


form {
  display: flex;
  flex-direction: column;
  align-items: center;
  max-width: 500px;
  margin: 0 auto;
  padding: 20px;
  border: 1px solid #ccc;
  border-radius: 5px;
}
label {
  display: block;
  margin-bottom: 10px;
  font-weight: bold;
}
input[type="text"],
input[type="email"],
textarea {
  width: 100%;
  padding: 10px;
  margin-bottom: 20px;
  border-radius: 5px;
  border: 1px solid #ccc;
}
textarea {
  height: 150px;
}
button[type="submit"] {
  background-color: #4CAF50;
  color: white;
  padding: 10px 20px;
  border: none;
  border-radius: 5px;
  cursor: pointer;
}
button[type="submit"]:hover {
  background-color: #3e8e41;
}
php forms

Create a php script code send-mail.php to handel the contact form, make sure that the file path for send-mail.php is correct and matches the location of the file.

PHP Script Code:

      <?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
  $to = 'youremail@example.com';
  $subject = 'New Contact Form Submission';
  $name = $_POST['name'];
  $email = $_POST['email'];
  $message = $_POST['message'];
  $headers = 'From: ' . $name . '<' . $email . '>';
  
  if (mail($to, $subject, $message, $headers)) {
    echo 'Message sent!';
  } else {
    echo 'Error sending message';
  }
}
?>

This PHP code processes the form data submitted by the user. It first checks whether the request method is "POST" by checking the $_SERVER['REQUEST_METHOD'] superglobal variable. If the method is "POST", it continues to process the form data.

The code initializes several variables, including the email address where the form submission will be sent ($to), the subject of the email ($subject), and the values of the form input fields ($name, $email, and $message).

The code then uses the mail() function to send an email with the form data to the specified email address. The mail() function requires four parameters: the recipient email address ($to), the email subject ($subject), the email message ($message), and any additional headers ($headers). The headers in this example include the name and email address of the form submitter.

Finally, the code uses a conditional statement to check whether the email was sent successfully. If the email was sent, it displays a success message. Otherwise, it displays an error message.


Example 2: login Form
HTML Form code
php forms

   <form method="post" action="login.php">
  <label for="username">Username:</label>
  <input type="text" name="username" id="username" required>

  <label for="password">Password:</label>
  <input type="password" name="password" id="password" required>
 <button type="submit">Log In</button>
</form>

Using the same CSS styling approach as before, in the contact form. with a input[type="password"] to style the password textarea liveeditor :


form {
  display: flex;
  flex-direction: column;
  align-items: center;
  max-width: 500px;
  margin: 0 auto;
  padding: 20px;
  border: 1px solid #ccc;
  border-radius: 5px;
}
label {
  display: block;
  margin-bottom: 10px;
  font-weight: bold;
}
input[type="text"],
input[type="password"],
input[type="email"],
textarea {
  width: 100%;
  padding: 10px;
  margin-bottom: 20px;
  border-radius: 5px;
  border: 1px solid #ccc;
}
textarea {
    width: 100%;
}
button[type="submit"] {
  background-color: #4CAF50;
  color: white;
  padding: 10px 20px;
  border: none;
  border-radius: 5px;
  cursor: pointer;
}
button[type="submit"]:hover {
  background-color: #3e8e41;
}
php forms

Create a php script code login.php to handel the login form, make sure that the file path for login.php is correct and matches the location of the file.

PHP script code

<?php
session_start();

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
  $username = $_POST['username'];
  $password = $_POST['password'];
  
  // Check if username and password are valid
  if ($username === 'user' && $password === 'password') {
    // Valid login
    $_SESSION['username'] = $username;
    header('Location: welcome.php');
  } else {
    // Invalid login
    $error = 'Invalid username or password';
  }
}
?>

This PHP code processes the form data submitted by the user. It first starts a session using the session_start() function.

The code then checks whether the request method is "POST" by checking the $_SERVER['REQUEST_METHOD'] superglobal variable. If the method is "POST", it retrieves the values of the username and password input fields using the $_POST superglobal variable.

Next, the code checks whether the username and password are valid. In this example, the username is "user" and the password is "password". If the login is valid, the code sets a session variable named username with the value of the username and redirects the user to a welcome page. If the login is invalid, the code sets an error message.


Example 3: File Upload Form:
php forms

  <form method="post" action="upload.php" enctype="multipart/form-data">
  <input type="file" name="file" required>
  <button type="submit">Upload</button> 
</form>

Adding some css code to style the form, This CSS code will give the form a modern look with a border, rounded corners, and padding. You can set colors and font sizes to your preference using the liveeditor :


form {
  display: flex;
  flex-direction: column;
  align-items: center;
  max-width: 500px;
  margin: 0 auto;
  padding: 20px;
  border: 1px solid #ccc;
  border-radius: 5px;
}
input[type="file"] {
  padding: 10px;
  margin-bottom: 20px;
  border-radius: 5px;
  border: 1px solid #ccc;
  background-color: #f8f8f8;
}
button[type="submit"] {
  background-color: #4CAF50;
  color: white;
  padding: 10px 20px;
  border: none;
  border-radius: 5px;
  cursor: pointer;
}
button[type="submit"]:hover {
  background-color: #3e8e41;
}
php forms

Create a php script code upload.php to handel the upload form, make sure that the file path for ipload.php is correct and matches the location of the file.


 <?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
  $upload_dir = 'uploads/';
  $filename = $_FILES['file']['name'];
  $filetmp = $_FILES['file']['tmp_name'];
  $filesize = $_FILES['file']['size'];
  
  if ($filesize > 1000000) { // limit file size to 1MB
    echo 'File size is too large';
    exit;
  }
$destination = $upload_dir . $filename;
  if (move_uploaded_file($filetmp, $destination)) {
    echo 'File uploaded successfully';
  } else {
    echo 'Error uploading file';
  }
}
?>

These are just a few examples of what you can do with PHP forms. There are many more possibilities, depending on your needs and the features you want to implement.
Need help , contact us.