Jump to content

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
?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.