Skip to content

How to use forms in PHP

Forms are the way the Web platform allows users to interact with a page and send data to the server.

Here is a simple form in HTML:

<form>
  <input type="text" name="name" />
  <input type="submit" />
</form>

You can put this in your index.php file like it was called index.html.

A PHP file assumes you write HTML in it with some “PHP sprinkles” using <?php ?>, so the Web Server can post that to the client. Sometimes the PHP part takes all of the page, and that’s when you generate all the HTML via PHP - it’s kind of the opposite of the approach we do here now.

So we have this index.php file that generates this form using plain HTML:

Pressing the Submit button will make a GET request to the same URL sending the data via query string, notice the URL changed to localhost:8888/?name=test

We can add some code to check if that parameter is set using the isset() function

<form>
  <input type="text" name="name" />
  <input type="submit" />
</form>

<?php
if (isset($_GET['name'])) {
  echo '<p>The name is ' . $_GET['name'];
}
?>

See? We can get the information from the GET request query string through $_GET.

What you usually do with forms however is, you perform a POST request:

<form **method="POST"**>
  <input type="text" name="name" />
  <input type="submit" />
</form>

<?php
if (isset($_POST['name'])) {
  echo '<p>The name is ' . $_POST['name'];
}
?>

See, now we got the same information but the URL didn’t change, the form information was not appended to the URL.

This is because we’re using a POST request, which sends the data to the server in a different way, through urlencoded data.

As mentioned, PHP will still serve the index.php file as we’re still sending data to the same URL the form is on.

We’re mixing a bunch of code and we could separate the form request handler from the code that generates the form.

So we can have in index.php this:

<form **method="POST" action="/post.php"**>
  <input type="text" name="name" />
  <input type="submit" />
</form>

and we can create a new post.php file with:

<?php
if (isset($_POST['name'])) {
  echo '<p>The name is ' . $_POST['name'];
}
?>

PHP will display this content now after we submit the form, because we set the action HTML attribute on the form.

This example is very simple, but the post.php file is where we could for example save the data to the database, or to a file.


→ Get my PHP Handbook

download all my books for free

  • javascript handbook
  • typescript handbook
  • css handbook
  • node.js handbook
  • astro handbook
  • html handbook
  • next.js pages router handbook
  • alpine.js handbook
  • htmx handbook
  • react handbook
  • sql handbook
  • git cheat sheet
  • laravel handbook
  • express handbook
  • swift handbook
  • go handbook
  • php handbook
  • python handbook
  • cli handbook
  • c handbook

subscribe to my newsletter to get them

Terms: by subscribing to the newsletter you agree the following terms and conditions and privacy policy. The aim of the newsletter is to keep you up to date about new tutorials, new book releases or courses organized by Flavio. If you wish to unsubscribe from the newsletter, you can click the unsubscribe link that's present at the bottom of each email, anytime. I will not communicate/spread/publish or otherwise give away your address. Your email address is the only personal information collected, and it's only collected for the primary purpose of keeping you informed through the newsletter. It's stored in a secure server based in the EU. You can contact Flavio by emailing [email protected]. These terms and conditions are governed by the laws in force in Italy and you unconditionally submit to the jurisdiction of the courts of Italy.

Related posts about php: