Page 1 of 1

Can someone help me with this?

Posted: Wed Sep 23, 2009 3:54 pm
by wb31
Hi everyone,

I have a problem that I can't figure out where I'm going wrong. I am trying to add a counter to the number of errors the user enters into the form. If the user enters something that is not numeric the counter should add one error per field. Can someone help me figure this out?

Thanks

Code: Select all

 
<html>
<head>
  <title></title>
</head>
 
<body style="font-family: Arial, Helvetica, sans-serif; color: black;">
<h1>My Bills</h1>
<form method=post>
 
<table>
 
<tr>
<th>Item</th>
<th>Amount</th>
</tr>
 
<?php
 
    $total = 0;
    $error_cnt = 0;
     
    
for ($i = 1; $i < 5; $i++)
{
    $item_name = 'item'.$i;
    $item_value = $_POST[$item_name];
    
    $amount = 'amount'.$i;
    $amount_value = $_POST[$amount]; 
    $amount_val = trim($amount_value);
    
    if (!empty($amount_val))
    {
            if (is_numeric($amount_val))
            {   
                print "Add total amount<br>";
                $total = $total + $amount_val;
            } 
            if (!is_numeric($amount_val))
            { 
                print "is not numeric number<br>";
                $error_cnt = 0;  
                $error_cnt++; //adds 1 to $error_cnt
                 
            }
     }    
     
    print "<tr><td><input type=text name=".$item_name." value='".$item_value."'></td>
           <td><input type=text name=".$amount." value='".$amount_value."'>
           <td></td></td></tr>\n";  
    
}
 
?>
 
</table>
 
<?php
  
   if (!empty($total))
   {
           if (is_numeric($total))
           {    
                print "Add total amount<br>";
                print "Total Bills:".$total;
           } 
           
           if (!is_numeric($total)) 
           {   
                
                print "Error count<br>"; 
                print "Errors:".$error_cnt;
           }
   } else {
        print "Total Bills: 0";
   } 
    
   
?>
<br><br><input type=submit value=Submit>
<br><br>
 
</form>
</body>
</html> 
 

Re: Can someone help me with this?

Posted: Wed Sep 23, 2009 4:14 pm
by Darhazer
You are reseting the counter each time:

Code: Select all

$error_cnt = 0; // set counter to zero
                $error_cnt++; // counter is always 1, because it was just set to 0

Re: Can someone help me with this?

Posted: Thu Sep 24, 2009 10:52 pm
by wb31
Thanks Darhazer for pointing that out.

I have another problem. I want to display a message next to the field where the use enters a non numeric character. Right now it is printing four times because of the for loop. How do I make the message only print next to the field that has the error.

Code: Select all

 
<html>
<head>
  <title></title>
</head>
 
<body style="font-family: Arial, Helvetica, sans-serif; color: black;">
<h1>My Bills</h1>
<form method=post>
 
<table>
 
<tr>
<th>Item</th>
<th>Amount</th>
</tr>
 
<?php
 
    $total = 0;
        $error_cnt = "";
    $amount_val = $error_text;
    $error_text = $error_str;
     
    
for ($i = 1; $i < 5; $i++)
{   
    
    $counter++;
    print "<br>i: $i";
    
    
    $item_name = 'item'.$i;
    $item_value = $_POST[$item_name];
    
    $amount = 'amount'.$i;
    $amount_value = $_POST[$amount]; 
    $amount_val = trim($amount_value);
    
    if (!empty($amount_val))
    {
            if (is_numeric($amount_val))
            {   
                print "Add total amount<br>";
                $total = $total + $amount_val;
            } 
            if (!is_numeric($amount_val))
            { 
                print "is not numeric number<br>";
                //error_str is printing in all four feilds 
                $error_str = "<font color=red>'$counter'Amount '$amount_val' is not a number</font>";
                $error_cnt++; //adds 1 to $error_cnt
            }
     }    
     
    print "<tr>";
    print "<td><input type=text name=".$item_name." value='".$item_value."'></td>\n";
    print "<td><input type=text name=".$amount." value='".$amount_value."'></td>\n";
    print "<td>$error_str</td>\n";
    print "</tr>\n";  
    
}
 
?>
 
</table>
 
<?php
   //need to work on getting this to switch right!
        
     if (!is_numeric($amount_val))      
     {   
        print "Error count<br>"; 
        //$error_str = "<font color=red>'$counter'Amount '$amount_val' is not a number</font>";
        print "<font color=red>Errors:$error_cnt</font>";
     }
    
 
    if (!empty($total))
    {       
        if (is_numeric($total))
        {    
                print "Add total amount<br>";
                print "Total Bills:".$total;
        }
   } else {
        print "Total Bills: 0";
   }
        
            
    
?>
<br><br><input type=submit value=Submit>
<br><br>
 
</form>
</body>
</html>                    
 

Re: Can someone help me with this?

Posted: Fri Sep 25, 2009 10:32 am
by wb31
anyone?

Re: Can someone help me with this?

Posted: Fri Sep 25, 2009 10:58 am
by Darhazer
You have to initialize the $error_str, otherwise it will appear for every field after the one that generated the error:

Code: Select all

for ($i = 1; $i < 5; $i++)
{  
    $counter++;
    $error_str = '';
 

Re: Can someone help me with this?

Posted: Fri Sep 25, 2009 11:24 am
by wb31
Thanks again Darhazer.

Re: Can someone help me with this?

Posted: Fri Sep 25, 2009 2:13 pm
by califdon
Two forum rules that will improve your chances of getting help here:

1. Use a meaningful Subject line! "Can someone help me with this?" results in most of the experienced forum members skipping your post.
2. When you have a second question unrelated to the first, start a new thread. "I have another problem" also results in most people never bothering to read it.