PHP forms and leaving blank fields

I am trying to create a simple PHP bill calculator... but am having is

Page 1 of 1

4 Replies - 2899 Views - Last Post: 21 July 2009 - 04:16 PM Rate Topic: -----

#1 sententia  Icon User is offline

  • New D.I.C Head

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

PHP forms and leaving blank fields

Posted 21 July 2009 - 03:08 PM

I am trying to create a PHP bill calculator... and have succeeded if the user inputs their information exactly how they should. Seeing as how this will never happen I need some advice.

I have a validation built in that requires the user to enter a number in the "Value" column, this validation works fine, however if the user leaves the "Item" field blank the program returns the error that the response is not a valid number.

How can I get the user to be able to leave the "Item" field blank without triggering the error that I have built into the program.

<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 (!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>
[


Is This A Good Question/Topic? 0
  • +

Replies To: PHP forms and leaving blank fields

#2 przemass  Icon User is offline

  • D.I.C Head

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

Re: PHP forms and leaving blank fields

Posted 21 July 2009 - 03:20 PM

Try
$item_value = isset($_POST[$item_name]) ? $_POST[$item_name] : null;
...
$item_total = isset($_POST[$item_amount]) ? $_POST[$item_amount] : null;
...
if (!empty($_POST) && !is_numeric($item_total)


Was This Post Helpful? 1
  • +
  • -

#3 sententia  Icon User is offline

  • New D.I.C Head

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

Re: PHP forms and leaving blank fields

Posted 21 July 2009 - 03:30 PM

Where does the if statement go?
Was This Post Helpful? 0
  • +
  • -

#4 przemass  Icon User is offline

  • D.I.C Head

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

Re: PHP forms and leaving blank fields

Posted 21 July 2009 - 03:35 PM

if (!is_numeric($item_total)

change to
if (!empty($_POST) && !is_numeric($item_total)

Was This Post Helpful? 1
  • +
  • -

#5 sententia  Icon User is offline

  • New D.I.C Head

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

Re: PHP forms and leaving blank fields

Posted 21 July 2009 - 04:16 PM

anyway that I could get this to work... i am able to leave the Item entry blank however when it posts it deletes the first input:

<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)){ //Cannot seem to get this section working
						
					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>


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1