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:
<html> <head> <title>PHP Test</title> </head> <body> <?php echo '<p>Hello World</p>'; ?> </body> </html>
The echo
PHP tag sort of... well... echoes the following code. We're gonna need that, but delete the php
code anyways.
First we make a new file, called result.php
. If you don't know how, press the new file button.
Once you have made the file, go back to index.php
and add this to your body:
<form action="" method=""> </form>
This is our form. All inputs will be inside there.
Now, let's add stuff to our form! We use this code for inputs:
<input>
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:
<form action="" method=""> Name: <input type="text"> Age: <input type="number"> </form>
Here are some other attributes you can add to your <input>
:
- 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:
<input type="text" disabled>
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.
The 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:
<html> <head> <title>PHP Form Handling</title> </head> <body> <form action="" method=""> Name: <input type="text" name="name"> Age: <input type="number" name="age"> </form> </body> </html>
Perfect. Now we will talk about action
and method
.
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, result.php
.
Method is how we get our PHP code. It can be in the URL bar:
Or just when you submit the form.
We'll use GET
, since I really don't care. But if you don't want it in the URL bar, just change it POST
.
Now comes our PHP.
In our result.php
file, we'll add the code for php:
<?php ?>
Remember how I said we will use the echo
PHP tag? Well, now we are. Add echo
in the middle of your <?php
and ?>
, like this:
<?php echo ?>
Now, how do we get the data in the form? Well, using $_GET
and $_POST
, we will echo that. This is how you do it:
If you used GET
for your form method, then this is your code:
<?php echo $_GET["the name of your input"]; ?>
And if you used POST
for your form method:
<?php echo $_POST["the name of your input"]; ?>
WAIT!
Don't run your code yet!
First, we should make it that way the user knows his input. Look:
Your name is <?php echo $_GET["name"]; ?>! You are <?php echo $_GET["age"]; ?> years old.
I feel like we forgot something...
OH YEAH!
The submit button!
Don't worry, we're almost at the end...
In your index.php
file, add a NEW input, with the value submit
:
<html> <head> <title>PHP Form Handling</title> </head> <body> <form action="result.php" method="GET"> Name: <input type="text" name="name"> Age: <input type="number" name="age"> <input type="submit"> </form> </body> </html>
PRO TIP: Don't like your submit button just saying "Submit"? Use the value
attribute!
There. Open your form, and check it out!
Then press submit and...
BAM!
It echoes the data collected in the form!
Here's a tip: Add the required
attribute to make the option required, like this:
<input type="text" required>
Well, that's the end of this tutorial! Share your forms in the comments!
Down below is the example I made:
Nice!
@DynamicSquid Thank you!
nice, this is my full php/laravel site https://www.vidbid.ir, I also have a fast YouTube converter
It's much helpful thanks a lot !!
How can I store what is inputted?
@piphi Maybe localStorage, if you are looking for a database, I don't know.
i need help, it's just showing a blank screen with no data....
@proinject Send URL.
@proinject Look at the beginning of my PHP tutorial.
@proinject Yeah...
Sad, how HTML doesn't support PHP...
You need to make a PHP repl and then add HTML files.