Can someone help me with this?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
wb31
Forum Newbie
Posts: 7
Joined: Wed Sep 09, 2009 1:34 pm

Can someone help me with this?

Post 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> 
 
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: Can someone help me with this?

Post 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
wb31
Forum Newbie
Posts: 7
Joined: Wed Sep 09, 2009 1:34 pm

Re: Can someone help me with this?

Post 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>                    
 
wb31
Forum Newbie
Posts: 7
Joined: Wed Sep 09, 2009 1:34 pm

Re: Can someone help me with this?

Post by wb31 »

anyone?
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: Can someone help me with this?

Post 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 = '';
 
wb31
Forum Newbie
Posts: 7
Joined: Wed Sep 09, 2009 1:34 pm

Re: Can someone help me with this?

Post by wb31 »

Thanks again Darhazer.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Can someone help me with this?

Post 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.
Post Reply