Skip to content
← Back to Community
PHP Form Handling Tutorial!
Profile icon
Whacko

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.
image
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:
    image
  • 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:
image
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:
image
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!
image
Then press submit and...
BAM!
It echoes the data collected in the form!
image
Here's a tip: Add the required attribute to make the option required, like this:

<input type="text" required>

Output:
image

Well, that's the end of this tutorial! Share your forms in the comments!

Down below is the example I made:

Voters
Profile icon
ZaidMouali
Profile icon
FredericBui
Profile icon
mrsgrivell
Profile icon
TitanJazza
Profile icon
piphi
Profile icon
whattocode
Profile icon
Kookiez
Profile icon
zplusfour
Profile icon
AsDoNa
Profile icon
Is122528
Comments
hotnewtop
Profile icon
ch1ck3n
Profile icon
thewaydu

Now that is great!

Profile icon
Whacko

@thewaydu Thank you!

Profile icon
DynamicSquid

Nice!

Profile icon
Whacko

@DynamicSquid Thank you!

Profile icon
[deleted]

This helped me so much!!!

Profile icon
Whacko

AMAZING FORM @xmasboy! I love it!

Profile icon
VidbidAdmin

nice, this is my full php/laravel site https://www.vidbid.ir, I also have a fast YouTube converter

Profile icon
Aasemasheikh

It's much helpful thanks a lot !!

Profile icon
Whacko
Profile icon
piphi

How can I store what is inputted?

Profile icon
Whacko

@piphi Maybe localStorage, if you are looking for a database, I don't know.

Profile icon
mobaja

i need help, it's just showing a blank screen with no data....

Profile icon
Whacko

@proinject Send URL.

Profile icon
mobaja
Profile icon
mobaja

@Whacko did I use the wrong language selection? am I not supposed to choose "HTML, CSS, JavaScript"?

Profile icon
Whacko

@proinject Look at the beginning of my PHP tutorial.

Profile icon
mobaja

@Whacko Im sorry for being dumb.); :DDDDD

Profile icon
mobaja

@Whacko thanks

Profile icon
Whacko

@proinject Yeah...
Sad, how HTML doesn't support PHP...
You need to make a PHP repl and then add HTML files.

Profile icon
mobaja

@Whacko thanks homie. im wondering if i can use CSS. i guess i will figure out now