/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] Checking required fields fails if value is 0

I wrote a function to check for required fields in a form, however when the value is 0 it fails. I wrote this page to test it, and its working in the broken manner [url]www.noas.com/test.php[/url]

[code=php]
<?php

function required($fields) {
$error = 0;
if( is_array($fields) ) {
foreach( $fields as $field ) {
if( !isset($_POST[$field]) || empty($_POST[$field]) )
$error++;
}
} else {
if( !isset($_POST[$fields]) || empty($_POST[$fields]) )
$error++;
}
if( $error === 0 )
return TRUE;
return FALSE;
}

$req = array(‘dname’,’gc’,’ilive’);
if( required($req) )
echo ‘Success!’;
else
echo ‘Failure!’;

echo’
<form action=”” method=”post” name=”add_item”>
<label for=”dname”>Name</label>
<input type=”text” name=”dname” value=”‘.(isset($_POST[‘dname’])?$_POST[‘dname’]:”).'” /><br />
<label for=”ilive”>Live Auction</label>
<input type=”radio” name=”ilive” value=”1″ ‘.(isset($_POST[‘ilive’])&&$_POST[‘ilive’]==’1′?’checked’:”).’/><span class=”checkbox”> Yes</span><br />
<input type=”radio” name=”ilive” value=”0″ ‘.(isset($_POST[‘ilive’])&&$_POST[‘ilive’]==’0′?’checked’:”).’/><span class=”checkbox”> No</span><br />
<label for=”gc”>Gift Certificate</label>
<input type=”radio” name=”gc” value=”0″ ‘.(isset($_POST[‘gc’])&&$_POST[‘gc’]==’0′?’checked’:”).’/><span class=”checkbox”> None</span><br />
<input type=”radio” name=”gc” value=”1″ ‘.(isset($_POST[‘gc’])&&$_POST[‘gc’]==’1′?’checked’:”).’/><span class=”checkbox”> Attached</span><br />
<input type=”radio” name=”gc” value=”2″ ‘.(isset($_POST[‘gc’])&&$_POST[‘gc’]==’2′?’checked’:”).’/><span class=”checkbox”> Will Send</span><br />
<input type=”radio” name=”gc” value=”3″ ‘.(isset($_POST[‘gc’])&&$_POST[‘gc’]==’3′?’checked’:”).’/><span class=”checkbox”> Noas to make</span><br />
<label for=”submit”>&nbsp;</label>
<input type=”submit” name=”submit” value=”submit” /> <input type=”reset”/>
</form>’;
[/code]

I’m assuming its due to empty returning true when the value is 0, how do I get around this?

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@NoasITauthorAug 11.2011 — Found the following function while searching to replace the empty call.

[code=php]
function is_empty($var, $allow_false = false, $allow_ws = false) {
if (!isset($var) || is_null($var) || ($allow_ws == false && trim($var) == "" && !is_bool($var)) || ($allow_false === false && is_bool($var) && $var === false) || (is_array($var) && empty($var))) {

return true;
} else {
return false;
}
}
[/code]


Hope that helps someone else.
Copy linkTweet thisAlerts:
@NoasITauthorAug 11.2011 — My original function now looks like:
[code=php]
function required($fields) {
if( is_array($fields) ) {
foreach( $fields as $field ) {
if( !isset($_POST[$field]) || is_empty($_POST[$field]) )
return FALSE;
}
} else {
if( !isset($_POST[$fields]) || is_empty($_POST[$fields]) )
return FALSE;
}
return TRUE;
}
[/code]


HTH
×

Success!

Help @NoasIT spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.1,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...