How to make a PHP sending form

"Build a PHP form submission with a step-by-step example - from creating a form to sending data to a script for processing."

Creating a PHP Sending Form

A PHP sending form is a great way to collect data from visitors on your website and store it in a database. It is also a convenient way to send an email to a user or an admin when a certain user action is taken. In this tutorial, we will show you how to build a simple PHP sending form that can be used to either send an email or store data in a database.

Step 1: Create the HTML Form

The first step is to create the HTML form that the user will fill out. Below is an example of the HTML code for a basic contact form with two fields: a name and an email address.

<form action="send.php" method="post">
  <p>
    <label for="name">Name:</label>
    <input type="text" name="name" id="name">
  </p>
  <p>
    <label for="email">Email:</label>
    <input type="text" name="email" id="email">
  </p>
  <p>
    <input type="submit" name="submit" value="Submit">
  </p>
</form>

This simple form will allow us to collect the user’s name and email address. The action attribute of the form is set to send.php, which will be the file we will use to process the form data.

Step 2: Create the PHP Script

Now that we’ve created the HTML form, we need to create the PHP script that will process the form data. This script will be responsible for taking the form data and either sending an email or storing it in a database.

The code below is an example of a simple PHP script that takes the form data and sends an email. This script will use the PHP mail() function to send an email to the specified email address. It also checks for errors, and if any are found, it will display an error message.


<?php
  // Check for empty fields
  if(empty($_POST['name'])      ||
     empty($_POST['email'])     ||
     !filter_var($_POST['email'],FILTER_VALIDATE_EMAIL))
     {
     echo "No arguments Provided!";
     return false;
   }
   
  $name = strip_tags(htmlspecialchars($_POST['name']));
  $email_address = strip_tags(htmlspecialchars($_POST['email']));
   
  // Create the email and send the message
  $to = '[email protected]'; // Add your email address inbetween the '' replacing [email protected] - This is where the form will send a message to.
  $email_subject = "Website Contact Form:  $name";
  $email_body = "You have received a new message from your website contact form.nn"."Here are the details:nnName: $namennEmail: $email_addressnn";
  $headers = "From: [email protected]"; // This is the email address the generated message will be from. We recommend using something like [email protected].
  $headers .= "Reply-To: $email_address";   
  mail($to,$email_subject,$email_body,$headers);
  return true;         
?>

Once the script is completed, you can upload it to your web server and link it to the HTML form in the action attribute.

Step 3: Test the Form

Now that you have created the HTML form and the PHP script, it’s time to test the form to make sure it works as expected. Fill out the form and submit it. If everything works as expected, you should receive an email with the form data.

Congratulations! You have successfully created a PHP sending form.

Answers (0)