This form adds the values when submitted and returns the value at the bottom.
Item Name is what is describes the name of the item... the Item Value must be a number.
I have the item value validation working as it returns an error if the user does not enter a number in the right side of the form.
However, I also want the user to be able to leave the Item Name value blank, meaning that they do not have to enter a value in every input and will not recieve an error.
I am using the
(!empty)function to try and get this to work, and it currently is working somewhat...
The issue is that when the user leaves the Item Name input blank, when the form is returned the Item Value input disappears, which it should not.
Any help would be greatly appreciated, basically my code works except when the user leaves the Item Name field blank, the Item Value field disappears when the PHP function is performed.
<form method=post>
<table>
<tr><th>Item</th><th>Value</th></tr>
<?php
$total = 0;
$error_cnt = 0;
for ($i=1; $i<5; $i++)
{
$item_name = 'item'."$i";
$item_value = $_POST[$item_name];
$item_amount = 'amount'."$i";
$item_total = $_POST[$item_amount];
$total = $total + $item_total;
$item_total = trim($item_total);
//VALIDATIONS NEED TO GO HERE
print '<tr>';
print "<td><input type=text name=$item_name value='$item_value'></td>\n";
if (!empty($item_value))//why wont this work
{
if (!is_numeric($item_total))
{
$error_cnt++; //adds 1 to $error_cnt
print
"<td><input type=text name=$item_amount value='$item_total'></td>\n
<td style=\"font-weight:bold; color:red;\">$item_total is not a number</td>";
}
else
{
print
"<td><input type=text name=$item_amount value='$item_total'></td>\n";
}
print '</tr>';
}
}
?>
</table>
<br />
<?php
if (!$error_cnt)
{
print "Total: $total";
}
else
{
print "<span style=\"font-weight:bold; color:red;\">Total Errors: $error_cnt</span>";
}
?>
<br><br />
<input type=submit value=Submit>
<br><br>
</form>
This post has been edited by sententia: 23 July 2009 - 04:09 PM

New Topic/Question
Reply




MultiQuote



|