PHP Form: POST, GET and REQUEST Methods

Quick Summary

  • PHP puts submitted form fields in $_GET for method get, $_POST for method post, and $_REQUEST for either method.
  • $_GET values sit in the bookmarkable URL query string, while $_POST values travel in the request body and suit forms that save data.
  • Each value is passed through htmlspecialchars before it's printed, so HTML or JavaScript typed into a field shows as text instead of running.

When an HTML form is submitted, PHP puts the field values into superglobal arrays. Which one you read depends on the form’s method: $_GET for method="get", $_POST for method="post", and $_REQUEST for either. This snippet prints a first and last name from each of them.

It expects a form with two inputs named fname and lname. Put the code in the PHP file the form submits to.

PHP
<!-- PHP GET METHOD -->
<p>
<?php if(isset($_GET['fname']) && !empty($_GET['fname'])): ?>
    <b>First Name:</b> <?php echo htmlspecialchars($_GET['fname']); ?></br>
<?php endif; ?>

<?php if(isset($_GET['lname']) && !empty($_GET['lname'])): ?>
    <b>Last Name:</b> <?php echo htmlspecialchars($_GET['lname']); ?>
<?php endif; ?>
</p>

<!-- PHP POST METHOD -->
<p>
<?php if(isset($_POST['fname']) && !empty($_POST['fname'])): ?>
    <b>First Name:</b> <?php echo htmlspecialchars($_POST['fname']); ?></br>
<?php endif; ?>

<?php if(isset($_POST['lname']) && !empty($_POST['lname'])): ?>
    <b>Last Name:</b> <?php echo htmlspecialchars($_POST['lname']); ?>
<?php endif; ?>
</p>

<!-- PHP REQUEST METHOD -->
<p>
<?php if(isset($_REQUEST['fname']) && !empty($_REQUEST['fname'])): ?>
    <b>First Name:</b> <?php echo htmlspecialchars($_REQUEST['fname']); ?></br>
<?php endif; ?>

<?php if(isset($_REQUEST['lname']) && !empty($_REQUEST['lname'])): ?>
    <b>Last Name:</b> <?php echo htmlspecialchars($_REQUEST['lname']); ?>
<?php endif; ?>
</p>

How it works

  • Each block checks that the field exists and isn’t empty before printing it, so a missing field doesn’t trigger a PHP warning. !empty() alone would do the same job, because empty() also returns true for a key that isn’t set.
  • $_GET values come from the URL query string, for example ?fname=Jeff&lname=Smith. They’re visible and can be bookmarked, which suits searches and filters.
  • $_POST values are sent in the request body and don’t appear in the URL. Use POST for forms that save data or contain passwords, and prevent resubmission when the page is refreshed so the data isn’t sent twice.
  • $_REQUEST merges GET and POST values (and, depending on the server’s PHP settings, cookies). It’s convenient, but you can’t tell where a value came from, so the specific arrays are usually the better choice.

Escape before you print

Every value goes through htmlspecialchars() before it’s echoed. It turns characters such as <, >, & and quotes into HTML entities, so if someone puts HTML or JavaScript into the URL, the page shows it as text instead of running it. Printing the raw value would open a cross-site scripting (XSS) hole. Inside WordPress, esc_html() does the same job. For cleaning values before you use or store them, see these PHP form sanitization examples.

Share on X (opens in a new tab) Share on LinkedIn (opens in a new tab) Share on Facebook (opens in a new tab)

ChatGPT (opens in a new tab) Claude (opens in a new tab) Gemini (opens in a new tab) Perplexity (opens in a new tab) Grok (opens in a new tab)

More Insights

See More