BaUn
BaUn

Reputation: 159

PHP: sane way of checking POST data?

I have page1.php that uses a form to send the following data:

<form action="page2.php" method="post">
<input type="text" name="f1">
<input type="text" name="f2">
<input type="text" name="f3">
<input type="text" name="f4">
<input type="submit" value="submit">
</form>

On page2.php, I do some basic validation so see that the form was actually submitted and that all data was entered:

<?php
if($_SERVER['REQUEST_METHOD'] != "POST" || empty($_POST["f1"]) || empty($_POST["f2"]) || empty($_POST["f3"]) || empty($_POST["f4"]))
{
    $missing_input = array();

    if (empty($_POST["f1"]))
    {
        $missing_input[] = "field1";
    }
    if (empty($_POST["f2"]))
    {
        $missing_input[] = "field2";
    }
    if (empty($_POST["f3"]))
    {
        $missing_input[] = "field3";
    }
    if (empty($_POST["f4"]))
    {
        $missing_input[] = "field4";
    }

    die("Error: " . implode(", ", $missing_input)");
}
?>

The problem is that the above feels very ugly and needs re-work when a new POST field is introduced in the page1.php form. How can I code this form validation better?

Upvotes: 1

Views: 1634

Answers (5)

Dennis
Dennis

Reputation: 14477

Store f1, f2, etc. in an array and use a for statement to iterate through the array.

Example:

if($_SERVER['REQUEST_METHOD'] != "POST")
    die("Error: Wrong method");
$fields = array("f1", "f2", "f3", "f4");
$field_names = array("field1", "field2", "field3", "field4");
$length = count($fields);
$missing_input = array();
for($i = 0; $i < $length; $i++)
    if(empty($_POST[$fields[$i]]))
        $missing_input[] = $field_names[$i];
if(!empty($missing_input))
    die("Error: " . implode(", ", $missing_input)");

Note: The above is untested.

Upvotes: 1

Your Common Sense
Your Common Sense

Reputation: 157870

above feels very ugly

That's wrong feeling.
If you need only check if field was empty, you can do some silly loop of course.

Bit in the real life there are different checks for the different fields, so, you have to write all them manually, in turn. There is nothing wrong with it.

the only issue I see here is redundant checks in the first line. why all these "|| empty" there?

Upvotes: 0

Thilo Savage
Thilo Savage

Reputation: 1071

This works for any number of fields you put into your form

if ($_SERVER['REQUEST_METHOD'] != "POST") {
    die("Improper request method");
}

if (!empty($_POST)) {
    foreach ($_POST as $key => $field) {
        if (strlen($field) === 0) {
            $missing_input[] = $key;
        }
    }
}   

if (!empty($missing_input)) {
    die("Error: " . implode(", ", $missing_input)");
}

Upvotes: 2

greg0ire
greg0ire

Reputation: 23255

You can use a simple loop with a variable holding fieldnames :

<?php
if ($_SERVER['REQUEST_METHOD'] == "POST")
{
  $fieldnames = array(
    'f1' => 'field1',
    'f2' => 'field2'
    'f3' => 'field3'
    'f4' => 'field4'
  );
  $missing_input = $array();
  foreach ($fieldnames as $code => $label)
  {
    if (empty($_POST[$code]))
      {
          $missing_input[] = $label;
      }
  }

  //Display errors nicely
  if (count($missing_input) > 0):?>
  <p>The following fields are required :</p>
  <ul>
    <?php foreach ($missing_input as $field): ?>
    <li><?php echo $field ?></li>
    <?php endforeach; ?>
  </ul>
  <?php endif;
}

Upvotes: 2

jakx
jakx

Reputation: 758

You should look at using a framework like zend or yii. The short answer is that if you don't like doing it this way you would have to code up a class or object to do this.

Upvotes: 1

Related Questions