Whats is the problem in this functions?
function validate_maxchars($t, $a, $alias, $required) {
if(strlen($t) <= $a) {
if(empty($t) && $required) {
return 'Field ' . $alias . ' is required.';
} else {
return true;
}
} else {
return 'Field ' . $alias . ' has a max of ' . $a . ' characters. You exceed the limit in ' . (strlen($t)-$a) . ' char(s).';
}
}
$err = 0;
$err= validate_maxchars($_POST['prod_name'], 22, 'Product Name', 0);
if($err != 1) { return $err; } else { $data['name'] = htmlentities($_POST['prod_name'], ENT_QUOTES); }
Show error:
<?php
if($err) {
?>
<div id="error" style="margin: 11px 5px 0 5px; padding: 9px; background: #eeb3b3; color: white; font-weight: bold; font-size: 11px; border: 1px solid #fd9797;"><?php echo $err; ?></div>
<?php
}
?>
When a submit this form I've got a blank page.
3 Answers 3
Add this:
error_reporting (-1);
ini_set('display_errors', 1);
answered Mar 16, 2011 at 14:36
powtac
41.1k28 gold badges120 silver badges173 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
It has to show nothing when there is no error ...
<?php
if($err) {
?>
<div id="error" style="margin: 11px 5px 0 5px; padding: 9px; background: #eeb3b3; color: white; font-weight: bold; font-size: 11px; border: 1px solid #fd9797;"><?php echo $err; ?></div>
<?php
}
else
echo $data['name'];
?>
answered Mar 16, 2011 at 5:50
sikas
5,55528 gold badges79 silver badges124 bronze badges
4 Comments
Mango
That's not the problem, when isn't a error is there a redirect to other page ... the problem is in the function, when there is error... i got blank page, like a error in the code.
sikas
@Mango: OK, well try using this code at the beginning of the page just for testing ...
<?php echo $err; exit(); ?> and make sure that the Error exists. Now when you run the page and make sure that the string has error, you should see the Error appears ... If nothing appears, try removing this line (or commenting it out) $err = 0;Mango
Nothing works, I reckon the problem is in the function, not in the error.
Mango
The problem is in that line: return $err; if change for echo $err; works, but not the way that a need. Any idea?
I could be wrong but try forcing error reporting by doing this at the beginning of file to show any possible errors or warnings.
error_reporting (E_ALL);
if( ! ini_get('display_errors') ) {
ini_set('display_errors', 1);
}
answered Mar 16, 2011 at 12:36
0xdeadbeef
4,1808 gold badges35 silver badges38 bronze badges
Comments
lang-php
var_dump($err)instead ofechoand report back the output.