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
[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”> </label>
<input type=”submit” name=”submit” value=”submit” /> <input type=”reset”/>
</form>’;
I’m assuming its due to empty returning true when the value is 0, how do I get around this?