Learn to Code via Tutorials on Repl.it!

← Back to all posts
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:

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:

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 <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:
  • Placeholder: Makes some light-gray text when the text box is blank.

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:

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:

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:

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:

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...
In your index.php file, add a NEW input, with the value submit:

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...
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:



Now that is great!


@thewaydu Thank you!




@DynamicSquid Thank you!


This helped me so much!!!


AMAZING FORM @xmasboy! I love it!


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.


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


@proinject Look at the beginning of my PHP tutorial.


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


@Whacko thanks


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


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