Build a first web application

This is a very basic introduction to building a web application using PHP. It assumes that you have some understanding of HTML and a web server that supports PHP to play with.

Forms

Let’s make a simple HTML page with a form.

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>My Web App</title>
  </head>
  <body>
    <h1>My Web App</h1>
    <form method="post">
      <div style="block">
        <label for="title">Title: </label>
        <input type="text id="title" name="title" size="50">
      </div>
      <div style="block">
        <label for="content">Content: </label><br>
        <textarea id="content" name="content" cols="50" rows="10"></textarea>
      </div>
      <input type="submit" id="submit" name="submit" value="Submit">
    </form>
  </body>
</html>Code language: HTML, XML (xml)

Click on Submit and…

Nothing happens!

Add some PHP

We can use PHP to display the data. PHP is enclosed in the <?php ?> tag.

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>My Web App</title>
  </head>
  <body>
    <h1>My Web App</h1>
    <form method="post">
      <div style="block">
        <label for="title">Title: </label>
        <input type="text id="title" name="title" size="50">
      </div>
      <div style="block">
        <label for="content">Content: </label><br>
        <textarea id="content" name="content" cols="50" rows="10"></textarea>
      </div>
      <input type="submit" id="submit" name="submit" value="Submit">
    </form>
    <?php
      if ($_POST['submit']) {
        echo "<h2>" . $_POST['title'] . "</h2>";
        echo "<pre>" . $_POST['content'] . "</pre>";
      }
    ?>
  </body>
</html>Code language: PHP (php)
Filed under: