Leaving Form Fields Blank

I am trying to creat a PHP form with two input fields: Item Name and I

Page 1 of 1

1 Replies - 1233 Views - Last Post: 24 July 2009 - 02:11 PM Rate Topic: -----

#1 sententia  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 21-July 09

Leaving Form Fields Blank

Posted 23 July 2009 - 03:39 PM

I am trying to creat a PHP form with two input fields: Item Name and Item Value...
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


Is This A Good Question/Topic? 0
  • +

Replies To: Leaving Form Fields Blank

#2 przemass  Icon User is offline

  • D.I.C Head

Reputation: 30
  • View blog
  • Posts: 166
  • Joined: 18-July 09

Re: Leaving Form Fields Blank

Posted 24 July 2009 - 02:11 PM

You are doing it wrong, too more complicated. Try this:
<form action="" method="post">
<table>
	<tr><th>Item</th><th>Value</th><th></th></tr>
<?php 
$err_number = 0;
$total_amount = 0;

for($i=0;$i<4;$i++) {
	//fields names
	$item_name = 'item'.$i;
	$value_name = 'value'.$i;
	
	//fields values
	$item_value = !empty($_POST[$item_name]) ? $_POST[$item_name] : '';
	$value_value = '';
	$err = false;
	
	//validation
	if (isset($_POST[$value_name])) {
		if (is_numeric($_POST[$value_name])) {
			$value_value = $_POST[$value_name];
			$total_amount += $value_value;
		} else {
			$err = true;
			$err_number++;
		}
	}
	
	echo '<tr><td><input type="text" name="'.$item_name.'" value="'.$item_value.'" /></td><td><input type="text" name="'.$value_name.'" value="'.$value_value.'" /></td><td>';
	
	if ($err === true) {
		echo '<b style="color:red">is not a number</b>';
	}
	
	echo '</td></tr>';
}
?>
	<tr><td colspan="3"><input type="submit" value="Send" /></td></tr>
	<tr><td colspan="3">
<?php
if ($err_number == 0) {
	echo 'Total: '.$total_amount;
} else {
	echo 'Errors number: '.$err_number;
}
?>
	</td></tr>
</table>
</form>
</form>


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1