php_nub_qq
php_nub_qq

Reputation: 16045

Undefined variables and indexes

I'm starting to get annoyed by having to check if all form fields have been filled every time with every single form. Since client side isn't reliable to have data validation on I need to do this server side with a thousand !empty's. Is there any way similar to throw-catch I can use to catch undefined variable or index errors?

Upvotes: 0

Views: 47

Answers (1)

hek2mgl
hek2mgl

Reputation: 158030

A very simple solution could look like this:

$indexes = array('foo1', 'foo2', '......');
foreach($indexes as $index) {
    if(!isset($_POST[$index])) {
        die('you are missing POST:' . $index);
    }
}

Mostly there are better ways to do that when using a framework. Like custom error messages for every param and validation. I once crafted a couple of classes for this on my own.

You wrote:

I'm starting to get annoyed by having to check if all form fields have been filled every time with every single form.

Although I understand what you mean ;) ,you should not get annoyed by this, as taking care about input data is one of the most important tasks in a web application. You should craft some classes for this like me or use a framework.

Upvotes: 3

Related Questions