Jump to content

[SOLVED] did the user fill out the entire form?


sjmiller

Recommended Posts

So I have what could be an extensive form that requires users to fill out all form inputs.  If even one of these inputs is missing, I need to have it return an error.  I find it somewhat inconvenient to go through 20-some isset()'s or empty()'s in one if() statement.  Is there any easy way to just count the $_POST array's variables that contain data without having to make an extensive one-by-one validation that each $_POST value has been filled out?  Thanks ahead of time for your help!

If you're going to validate a form do it right.

 

you could do a javascript validation before hand but you should always check incoming data..

 

// javascript form validator
function validate(TheForm) {
  for (i in TheForm.childNodes) {
    if (TheForm.childNodes[i].value == '' || (''+TheForm.childNodes[i].value+'') == 'undefined') { alert("ERROR!!"); return false; }
  }
  // if script reaches here without returning false than its obviously true..
  return true;
}

 

and then

 

<form action="whatever.php" method="POST" onsubmit="validate(this)">

  <input...

  ...

  ...

</form>

sorry :( I hit submit too fast!

 

and if I modify that 1 you won't read the php solution;

 

<?php
  foreach ($_POST as $k => $v) {
    // $k will be the field name, $v will be the value of the field
    if ($v == '') die("error");
  }
  // if script reaches here without die() than assume good values
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.