Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

PHP Simple HTML Form Handling With PHP

Ghost

Platinum Coder
If you are new to PHP, it is a great idea to learn how to do some basic form handling.
Although you should already know a bit about arrays, if/else statements, and variables, form handling is not too difficult to grasp. Let's start with some of the basics.

Below we have a basic HTML form. We start by declaring that the form is a POST, and it needs to pass data to newsletter_signup.php
We have two text fields. One is for their user's email address and the other is their full name.
At the bottom we have a button to submit the form to our PHP file.

HTML:
<form method="post" action="newsletter_signup.php">
Full Name <br>
<input type="text" name="full_name" maxlength="255" />
<br><br>
Email Address <br>
<input type="email" name="email_address" maxlength="255" />
<br><br>
<button type="submit">Join</button>
</form>

So, the next logical step is to create newsletter_signup.php, which is where we can handle all of the logic for our new sign up.
I will not be discussing how to connect to a database and save the information, or how to send a confirmation email for the new subscriber. This tutorial is simply here to show you how to accept the $_POST data sent from our form.

newsletter_signup.php
PHP:
<?php
   
    function checkEmail($address){ // This would probably be in a separate file
        if(trim($address) == "[email protected]"){
            // Instead of this we would check to see if the email is already subscribed in database instead of checking to see if it matches a specific email like in this example
            return false;
        }
        return true;
    }

    function signup($address, $name){
        // connect to DB
        // save to database
        // maybe send welcome email
       
        return true; // if saved successfully
    }


    if(!isset($_POST['full_name']) || empty(($name = FILTER_VAR(trim($_POST['full_name']), FILTER_SANITIZE_STRING))){
        exit("You must supply your full name to join the newsletter.");
    }
    if(!isset($_POST['email_address']) || $_POST['email_address'] == null || FILTER_VAR($_POST['email_address'], FILTER_VALIDATE_EMAIL) == false){
        exit("You must supply a valid email address to join the newsletter.");
    }
    if(!checkEmail($_POST['email_address'])){
        exit("You are already subscribed to our newsletter!");
    }

    if(signup($_POST['email_address'], $name)){
        exit("Thank you for subscribing to our newsletter.");
    } else {
        exit("There was an issue subscribing to our newsletter.");
    }

?>

I tried to leave comments, but I think this should be self explanatory. If not, here's some more details about what is going on.
When you submit an HTML form to a PHP controller with the POST Method, you have access to a PHP array called $_POST.
You can access the values by referring to them by the "name" attribute in the original HTML form. This is why we have $_POST['full_name'] and $_POST['email_address'].
We start by declaring two functions, and I'll discuss them more soon. We have one that would be responsible for checking a database to see if an email is already subscribed (the checkEmail() function) and another function to actually sign the user up (signup() function). These two functions could be normal codeblocks, but I want to show you the functions because sometimes you may have multiple ways for a user to join your newsletter or website, including options for apps separate from the normal website. For this reason and more, using functions is a great way to make sure your data handling is consistent. The functions should be in a separate file so they can be shared with other controllers, but at the very least you can get an idea of how it all works from this tutorial example.

Once we declared our functions, we immediately check to see if our required values are present.
If the name is not supplied (!isset check), and then does the following...
- sets $name variable = to the trimmed & string sanitized value of our full name
- checks to see if $name is empty (blank, false, null, etc)

Then we check if the email address is supplied & we also make sure that it is a valid email with another FILTER_VAR()
This is followed up by using our function to check if the email is already subscribed or not.
If the function returns false, we tell the user they are already subscribed.

Then we use our signup() function to make sure the user is subscribed & we return a response message based on whether that function is successful or not.

It should be noted that this whole PHP file & HTML form could benefit from some basic AJAX with JavaScript so that error/success messages can appear on the screen somewhere instead of just causing them to see a blank page with the error on screen. However, this tutorial is just for basic form handling, so I hope you can figure out the rest :)
 
Last edited:

New Threads

Latest posts

Buy us a coffee!

Back
Top Bottom