PHP Form Handling Tutorial!
Using PHP Web Server and HTML, we're gonna make ourselves a form and then get the data!
PHP isn't an easy language. It's got a lot of code, and it can get confusing. So you should follow along with this tutorial so you can get a nice form to share with family and friends!
Make a new PHP WEB SERVER repl, not a PHP CLI. Okay, so PHP is just HTML with PHP. You should see this:
echo PHP tag sort of... well... echoes the following code. We're gonna need that, but delete the
php code anyways.
This is our form. All inputs will be inside there.
Now, let's add stuff to our form! We use this code for inputs:
There is no
</input> needed. There is one super-important attribute: The
type. It changes the type of text box. Here are all the different input types:
- Text: A normal text box
- Number: A number text box
- Checkbox: A checkbox
- Color: A color picker
- Date: A date picker
- Datetime-local: A date and time picker
- Email: A email text box
- File: A file upload
- Hidden: A hidden text box
- Month: A month and date picker
- Password: A password text box
- Range: A slider
- Reset: A reset button that resets all your inputs
- Search: A search bar, with a little x
- Time: A time input
- Url: A url input
- Week: A week input
You can go ahead and play around with those inputs, but I'll just make a number input and a text input:
Here are some other attributes you can add to your
- Value: Will set the value of the input at the beginning:
- Disabled: Makes the input disabled. IMPORTANT NOTE: This is how you disable a text box:
REMEMBER: When refreshing your code in PHP, press the little refresh button:
Once you have finished adding all the inputs you want, you will add the
name attribute to each one.
name attribute is how we will get the data in each of them, so don't give two the same name. This is what my code looks like, I hope yours is alike:
Perfect. Now we will talk about
Action is where our PHP will go. It can be a link, or a file, like what we are doing. In the
action attribute, add your file,
Method is how we get our PHP code. It can be in the URL bar:
Or just when you submit the form.
GET, since I really don't care. But if you don't want it in the URL bar, just change it
Now comes our PHP.
result.php file, we'll add the code for php:
Remember how I said we will use the
echo PHP tag? Well, now we are. Add
echo in the middle of your
?>, like this:
Now, how do we get the data in the form? Well, using
$_POST, we will echo that. This is how you do it:
If you used
GET for your form method, then this is your code:
And if you used
POST for your form method:
Don't run your code yet!
First, we should make it that way the user knows his input. Look:
I feel like we forgot something...
The submit button!
Don't worry, we're almost at the end...
index.php file, add a NEW input, with the value
PRO TIP: Don't like your submit button just saying "Submit"? Use the
There. Open your form, and check it out!
Then press submit and...
It echoes the data collected in the form!
Here's a tip: Add the
required attribute to make the option required, like this:
Well, that's the end of this tutorial! Share your forms in the comments!
Down below is the example I made: