Can someone help me with this?
Posted: Wed Sep 23, 2009 3:54 pm
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
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>