Functions within functions not sending parameters?
Posted: Sun Jun 01, 2008 12:04 pm
The PHP below works out of the box except the multifunction function. Essentially my question is why are the parameters in the functions (within the parent function) not being set?
When I validate manually calling each function individually on lines 96~100 it works fine. However when I attempt to echo any return on the "super" function I get the error 'Empty regular expression' which I understand what it is and how it would be cause, yet I'm not sure why having the functions within a parent function would strip the parameters from the child functions?
Keep in mind I've made it easy to spot which text input contains the invalid data (just add another 0-f hex value to validate it of course (CSS color value)).
So validation works fine save when I attempt to group all the functions in to a single function.
When I validate manually calling each function individually on lines 96~100 it works fine. However when I attempt to echo any return on the "super" function I get the error 'Empty regular expression' which I understand what it is and how it would be cause, yet I'm not sure why having the functions within a parent function would strip the parameters from the child functions?
Keep in mind I've made it easy to spot which text input contains the invalid data (just add another 0-f hex value to validate it of course (CSS color value)).
So validation works fine save when I attempt to group all the functions in to a single function.
Code: Select all
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>PHP Serverside Validation of CSS post data</title>
<style type="text/css">
body,html {font-family: monospace;}
b {color: #00f;}
b.bad {color: #f00;}
b.good {color: #0f0;}
div {outline: #f00 solid 1px;}
div.overflow {float: left; font-size: 12px; height: 240px; margin: 4px; overflow: auto; width: 25%;}
form {float: left; width: 400px;}
form input {font-size: 10px; width: 60px;}
</style>
</head>
<body>
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
<fieldset>
<div>
<input name="ce000" value="body" />
<input name="ce001" value="444" />
<input name="ce002" value="11/002" />
<input name="ce003" value="00f" />
<input name="ce004" value="00f" />
</div>
<div>
<input name="ce010" value="h2, h2, h3, h4" />
<input name="ce011" value="transparent" />
<input name="ce012" value="none" />
<input name="ce013" value="1f1" />
<input name="ce014" value="115" />
</div>
<div>
<input name="ce020" value="div" />
<!-- Default Value below is invalid, add an extra digit (0-F Hex) to validate -->
<input name="ce021" style="background-color: #000; border-color: #f0f; color: #fff;" value="55555" />
<input name="ce022" value="01/002" />
<input name="ce023" value="5f6" />
<input name="ce024" value="5f6" />
</div>
<div>
<input name="ce030" value="span" />
<input name="ce031" value="7d6" />
<input name="ce032" value="" />
<input name="ce033" value="" />
<input name="ce034" value="" />
</div>
<br style="clear: both;" />
<input style="display: block; width: 60%;" type="submit" value="Validate Form Data" />
</fieldset>
</form>
<?php
function super_validate()
{
if (validate_clientside_array($regex_0_selectors,'0') != 'invalid'
&& validate_clientside_array($regex_1_colors,'1') != 'invalid'
&& validate_clientside_array($regex_2_bgimages,'2') != 'invalid'
&& validate_clientside_array($regex_1_colors,'3') != 'invalid'
&& validate_clientside_array($regex_1_colors,'4') != 'invalid'
) {return 'invalid';}
}
function validate_clientside_array($regex, $position)
{
$item = '/'.$position.'$/';
//echo 'parameter 1 = <b>'.$regex.'</b><br />';
//echo 'parameter 3 = <b>'.$position.'</b><br /><br />';
foreach($_POST as $key => $value)
if (preg_match($item, $key))
{
if ($validity != '1')
{
//if (preg_match($regex, $value)) {echo ' <b class="good">'.$key.' = '.$value.'</b> is a <b class="good">match</b>!</b><br />'."\n";}
//else {echo '<b class="bad">'.$key.' == '.$value.'</b> not a <b class="bad">match</b>!</b><br />'; $validity = '1'; return 'not valid';}
if (!preg_match($regex, $value)) {return 'invalid';}
/*return false;*/
}
}
}
$regex_0_selectors = '/([0-9A-z]([#|.|,|:][0-9A-z]){0,10})?$/';
$regex_1_colors = '/^(?:(?:[0-9a-f]{3}){1,2}|transparent)?$/';
$regex_2_bgimages = '/^(([0-9]{2})\/([0-9]{3})|none)?$/';
echo '<b> R E S U L T = </b>'.super_validate();
echo '<br style="clear: both;" />';
echo '<b> RESULT = </b> '.validate_clientside_array($regex_0_selectors,'0').'<br />';
echo '<b> RESULT = </b> '.validate_clientside_array($regex_2_bgimages,'2').'<br />';
echo '<b> RESULT = </b> '.validate_clientside_array($regex_1_colors,'1').'<br />';
echo '<b> RESULT = </b> '.validate_clientside_array($regex_1_colors,'3').'<br />';
echo '<b> RESULT = </b> '.validate_clientside_array($regex_1_colors,'4').'<br />';
if (validate_clientside_array($regex_0_selectors,'0') != 'invalid'
&& validate_clientside_array($regex_1_colors,'1') != 'invalid'
&& validate_clientside_array($regex_2_bgimages,'2') != 'invalid'
&& validate_clientside_array($regex_1_colors,'3') != 'invalid'
&& validate_clientside_array($regex_1_colors,'4') != 'invalid'
) {echo '<br />The array contains <b class="good">only valid data</b>.';}
else {echo '<br />The array <b class="bad">contains invalid data</b>!';}
?>
</body>
</html>