0

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.

asked Mar 16, 2011 at 5:44
2
  • 1
    checking if $t is empty below strlen check is quite nonsense Commented Mar 16, 2011 at 5:55
  • try var_dump($err) instead of echo and report back the output. Commented Mar 16, 2011 at 12:30

3 Answers 3

1

Add this:

error_reporting (-1);
ini_set('display_errors', 1);
answered Mar 16, 2011 at 14:36
Sign up to request clarification or add additional context in comments.

Comments

0

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

4 Comments

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.
@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;
Nothing works, I reckon the problem is in the function, not in the error.
The problem is in that line: return $err; if change for echo $err; works, but not the way that a need. Any idea?
0

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

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.