Form Handling — PHP Technical Interview Questions & Answers

Form Handling

Form Handling

  • An HTML form POSTs to a PHP file. <form method='post' action='save.php'> then in save.php: $name = $_POST['name'] ?? ''; Never trust it — trim, check empty, htmlspecialchars when you print back.
  • GET puts data in the URL (?name=Asha) — $_GET. Use POST for passwords and saves. After a successful POST, redirect (PRG) so refresh doesn’t save twice.
  • Trap: putting $_POST straight into SQL. Use prepared statements. Trap: printing $_POST into HTML without escape (XSS).

Technical Interview Tip: $_POST['name'] after a form. Validate + htmlspecialchars.

Code Examples for Form Handling

Form Handling — working example

<?php
// $name = $_POST["name"] ?? "";
$name = "Asha"; // demo input
$name = htmlspecialchars($name, ENT_QUOTES, "UTF-8");
echo "Hello, $name";

Form Handling — read request data carefully and escape output to prevent XSS.

How to Explain Form Handling to an Interviewer

GET vs POST?

GET in the URL POST in the body — use for login/save