HTTP Methods

HTTP GET ($_GET)


Back-end

codes/get-hello/hello.php:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Document</title>
</head>
<body>
  <div class="">
    <?php
      $name = $_GET['name'] ?? '';
      echo "Olá $name";
    ?>
  </div>
</body>
</html>

http://localhost:8080/php/web/http/codes/get-hello/hello.php?name=Alice:

$ curl 'http://localhost:8080/php/web/http/codes/get-hello/hello.php?name=Alice' \
  -H 'Connection: keep-alive' \
  -H 'Pragma: no-cache' \
  -H 'Cache-Control: no-cache' \
  -H 'Upgrade-Insecure-Requests: 1' \
  -H 'User-Agent: Mozilla/5.0 ... Chrome/67.0.3396.99 Safari/537.36' \
  -H 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8' \
  -H 'Accept-Encoding: gzip, deflate, br' \
  -H 'Accept-Language: en-US,en;q=0.9' \
  --compressed
┌───────────────────────────────────────────────────────────────────────────────────────────────┐
│ GET /php/web/http/codes/get-hello/hello.php?name=Alice HTTP/1.1                               │
│ Host: localhost:8080                                                                          │
│ Connection: keep-alive                                                                        │
│ Pragma: no-cache                                                                              │
│ Cache-Control: no-cache                                                                       │
│ Upgrade-Insecure-Requests: 1                                                                  │
│ User-Agent: Mozilla/5.0 ... Chrome/67.0.3396.99 Safari/537.36                                 │
│ Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8 │
│ Accept-Encoding: gzip, deflate, br                                                            │
│ Accept-Language: en-US,en;q=0.9                                                               │
├───────────────────────────────────────────────────────────────────────────────────────────────┤
└───────────────────────────────────────────────────────────────────────────────────────────────┘

Front-end

codes/get-hello/index.html:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Document</title>
</head>
<body>
  <form action="hello.php">
    <input type="text" name="name">
    <input type="submit" value="Olá...">
  </form>
</body>
</html>

http://localhost:8080/php/web/http/codes/get-hello/

References

HTTP POST ($_POST)


Back-end

codes/post-hello/hello.php:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Document</title>
</head>
<body>
  <div class="">
    <?php
      $name = $_POST['name'] ?? '';
      echo "Olá $name";
    ?>
  </div>
</body>
</html>

http://localhost:8080/php/web/http/codes/post-hello/hello.php:

$ curl 'http://localhost:8080/php/web/http/codes/post-hello/hello.php' \
  -H 'Connection: keep-alive' \
  -H 'Pragma: no-cache' \
  -H 'Cache-Control: no-cache' \
  -H 'Origin: http://localhost:8080' \
  -H 'Upgrade-Insecure-Requests: 1' \
  -H 'Content-Type: application/x-www-form-urlencoded' \
  -H 'User-Agent: Mozilla/5.0 ... Chrome/67.0.3396.99 Safari/537.36' \
  -H 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8' \
  -H 'Referer: http://localhost:8080/php/web/http/codes/post-hello/' \
  -H 'Accept-Encoding: gzip, deflate, br' \
  -H 'Accept-Language: en-US,en;q=0.9' \
  --data 'name=Alice' \
  --compressed
┌───────────────────────────────────────────────────────────────────────────────────────────────┐
│ POST /php/web/http/codes/post-hello/hello.php HTTP/1.1                                        │
│ Host: localhost:8080                                                                          │
│ Connection: keep-alive                                                                        │
│ Content-Length: 10                                                                            │
│ Pragma: no-cache                                                                              │
│ Cache-Control: no-cache                                                                       │
│ Origin: http://localhost:8080                                                                 │
│ Upgrade-Insecure-Requests: 1                                                                  │
│ Content-Type: application/x-www-form-urlencoded                                               │
│ User-Agent: Mozilla/5.0 ... Chrome/67.0.3396.99 Safari/537.36                                 │
│ Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8 │
│ Referer: http://localhost:8080/php/web/http/codes/post-hello/                                 │
│ Accept-Encoding: gzip, deflate, br                                                            │
│ Accept-Language: en-US,en;q=0.9                                                               │
├───────────────────────────────────────────────────────────────────────────────────────────────┤
│ name=Alice                                                                                    │
└───────────────────────────────────────────────────────────────────────────────────────────────┘

Front-end

codes/post-hello/index.html:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Document</title>
</head>
<body>
  <form action="hello.php" method="post">
    <input type="text" name="name">
    <input type="submit" value="Olá...">
  </form>
</body>
</html>

http://localhost:8080/php/web/http/codes/post-hello/

References

Simple Form


codes/simple-form/index.php:

<?php
$dias = ["Segunda", "Terça", "Quarta", "Quinta", "Sexta"];
$cursos = [
  "TSI" => "Sistemas para Internet",
  "RC" => "Redes de Computadores"
];
?>
<!doctype html>
<html lang="en">

<head>
  <!-- Required meta tags -->
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

  <!-- Bootstrap CSS -->
  <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">

  <title>Simple Form</title>
  <style>
    body {
      color: #606c71;
    }
  </style>
</head>

<body>
  <div class="container mt-4">
    <h1>Contato</h1>
    <form action="review.php" method="post" class="mt-4">
      <div class="form-group">
        <label for="nome">Nome</label>
        <input type="text" class="form-control" id="nome" name="nome" required autofocus>
      </div>
      <div class="form-group">
        Sexo
        <div>
          <input type="radio" name="sexo" id="masculino" value="Masculino" checked>
          <label for="masculino" class="pr-1">Masculino</label>
          <input type="radio" name="sexo" id="feminino" value="Feminino">
          <label for="feminino">Feminino</label>
        </div>
      </div>
      <div class="form-group">
        Estou no IFPB:
        <div>
          <?php
          $inputs = "";
          foreach ($dias as $dia) {
            $inputs .= "<input type=\"checkbox\" name=\"dias[]\" id=\"${dia}\" value=\"${dia}\">
            <label for=\"${dia}\" class=\"pr-3\">${dia}</label>";
          }
          echo $inputs;
          ?>
        </div>
      </div>
      <div class="form-group">
        <label for="curso">Curso</label>
        <select name="curso" class="form-control" id="curso">
          <option value="" selected>escolha um curso</option>
          <?php
          $options = "";
          foreach ($cursos as $sigla => $curso) {
            $options .= "<option value=\"${sigla}\">${curso}</option>";
          }
          echo $options;
          ?>
        </select>
      </div>
      <div class="form-group">
        <label for="email">Email</label>
        <input type="text" class="form-control" id="email" name="email">
      </div>
      <div class="form-group">
        <label for="titulo">Título</label>
        <input type="text" class="form-control" id="titulo" name="titulo">
      </div>
      <div class="form-group">
        <label for="mensagem">Mensagem</label>
        <textarea id="mensagem" class="form-control" name="mensagem"></textarea>
      </div>
      <input type="submit" value="Enviar">
    </form>
  </div>

  <!-- Optional JavaScript -->
  <!-- jQuery first, then Popper.js, then Bootstrap JS -->
  <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
  <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
</body>

</html>

codes/simple-form/review.php:

<!doctype html>
<html lang="en">

<head>
  <!-- Required meta tags -->
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

  <!-- Bootstrap CSS -->
  <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">

  <title>Simple Form</title>
  <style>
    body {
      color: #606c71;
    }
  </style>
</head>

<body>
  <div class="container mt-4">
    <h1>Contato</h1>
    <table class="table mt-4">
      <tbody>
        <?php
        // var_dump($_POST);
        $rows = '';
        foreach ($_POST as $field => $value) {
          $field = ucfirst($field);
          if ($field === "Dias") {
            $field = "Dias no IFPB";
            $value = join(", ", $value);
          }
          $rows .= "<tr>
              <th class=\"w-25\">${field}:</th>
              <td>${value}</td>
            </tr>";
        }
        echo $rows;
        ?>
      </tbody>
    </table>
  </div>

  <!-- Optional JavaScript -->
  <!-- jQuery first, then Popper.js, then Bootstrap JS -->
  <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
  <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
</body>

</html>

http://localhost:8080/php/web/http/codes/simple-form/

References