PHP简单发票收据

时间:2015-07-24 04:03:08

标签: php

我想从快餐店买一张简单的收据 JollyMenu.php



<form action="showReceipt.php" method="POST">
<table >
<tr><td>Transaction ID <input type="text" name="txtID"> <?php echo date("m / d / Y");?></td></tr>
<tr><td><h2>Your Order:</h2></td></tr>
<tr><td><input  type="checkbox"  name="cbItem[]" value="Chicken Joy"> Chicken Joy (PhP 90.00)</td> <td>Quantity <input type="text" name="txtQty[]"></td></tr>
<tr><td><input  type="checkbox" name="cbItem[]" value="Jolly Spaghetti"> Jolly Spaghetti (PhP 50.00)</td> <td>Quantity <input type="text" name="txtQty[]"></td></tr>
<tr><td><input type="checkbox" name="cbItem[]" value="Yum Burger"> Yum Burger (PhP 29.00)</td> <td>Quantity <input type="text" name="txtQty[]"></td></tr>
<tr><td><input type="checkbox" name="cbItem[]" value="Jolly Twirls"> Jolly Twirls (PhP 25.00)</td> <td>Quantity <input type="text" name="txtQty[]"></td></tr>
<tr><td><input type="checkbox" name="cbItem[]" value="Big Champ"> Big Champ (PhP 120.00)</td> <td>Quantity <input type="text" name="txtQty[]"></td></tr>
<tr><td>Amount Given: <input type="text" name="txtAmount"></td> <td><input type="submit" name="btnGen" value="Generate Receipt">  <input type="reset" value="Clear">


</table>
</form>
&#13;
&#13;
&#13; ShowReceipt.php

&#13;
&#13;
<html>
<body>


<?php
		
error_reporting(0);	
echo '<p align="center">JollyBee Food Corporation</br>
		1/F Kalentong cor. Shaw Blvd. Mandaluyong City 550</br>
			</br></p></br>';

$item=$_POST['cbItem'];
$qty=$_POST['txtQty'];
	
if(isset($_POST['btnGen'])){    	
{
	
foreach($item as $selected)
{	
	     	
	if($selected=='Chicken Joy')
	{
		$price = 90.00;
		$total += 90.00;
		
	}
	else if($selected=='Jolly Spaghetti')
	{
		$price = 50.00;
$total += 90.00;
		
	}
	else if($selected=='Yum Burger')
	{
		$price = 29.00;
		$total += 29.00;
	}
	else if($selected=='Jolly Twirls')
	{
		$price = 25.00;
		$total += 25.00;
	}
	else if($selected =='Big Champ')
	{
		$price = 120.00;
		$total += 120.00;
	}
	    	    	
echo $selected.$_POST["txtQty"][$i].'<br>';
echo $price.'<br>';
	
}
}
}


echo $total;
	    		    	  		  	    		
?>

</body>
</html>
&#13;
&#13;
&#13;

我还需要获取数量的文本框的值 乘以已经选择的项目数。如何获得每个循环中运行的文本框的值。?

1 个答案:

答案 0 :(得分:0)

我建议为每个项目和数量字段使用唯一的产品ID。问题是,当检查所有项目时,cbItem和txtQty键只能正确匹配。我认为最快/最简单的解决方案是在关联的cbItem VALUE上键入每个txtQty字段名称。所以第一次

<input type="text" name="txtQty[]">

会变成:

<input type="text" name="txtQty[Chicken Joy]">

然后在PHP代码中,替换每组:

$price = 90.00;
$total += 90.00;

使用:

$price = 90;
$total += ($price * max($qty[$selected], 1)); // ensure at least qty of 1