1

我试图让我的复选框在页面底部输出“Korting is ... %”,具体取决于选中哪个复选框,例如选中第 2 个和第 3 个框会输出“Korting is 15%”但我不能似乎让它工作。有人可以帮我提前谢谢你

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="nl">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>XXL Computer winkel</title>
</head>
<body>
<h3>php lab 04</h3>

<table border=0 cellpadding=0 cellspacing=0 width=100%>
<form name="orderform" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<tr> 
<td> 
Korting:<br />
<input type="checkbox" name="korting" value="15" /> Student 15% <br/>
<input type="checkbox" name="korting" value= "10"  /> Senior 10% <br/>
<input type="checkbox" name="korting" value= "5" /> klant 5% <br/> 
<hr/>
<img src="images/Lab4/1.jpg" width="200px" height="200px" alt=" "/>
<td/>
</tr>     
<tr> 
<td>
Toshiba Satellite A100-510 basisprijs 999.99
</td>
</tr>

<tr>     
<td><!-- Shopping cart begin-->
<input type="hidden" name="toshibaproduct" value="001"/>
<input type="hidden" name="toshibamerk" value="Toshiba"/>
<input type="hidden" name="toshibamodel" value="Sattelite A100-510"/>
Aantaal: <input type="text" size=2 maxlenght=3 name="toshibaaantal" value="0"/>
<input type="hidden" name="toshibaprijs" value="999.99"/>

<input type="image" src="images/Lab4/2.jpg" border=0 value="bestellen"/>
<hr/>
</td><!--Shopping Cart END -->
</tr>
</form>
</table> 
</body>
</html>
4

2 回答 2

1

You need to use a name ending in [] such as name="korting[]". Then you can use $_POST['korting'] which will be an array containig the values of the checked checkboxes.

Note that $_POST['korting'] will not be set at all if no checkbox is checked!

于 2013-01-20T14:31:51.630 回答
0
  • 如果您想选择一个选项,请使用radio而不是checkbox
  • 不要使用 PHP_SELF,这是一个安全漏洞。反而:<form action="">
  • 访问值$_POST['korting']

总结所有值,使您的表单如下所示:

<input type="checkbox" name="korting[]" value="15" /> Student 15% <br/>
<input type="checkbox" name="korting[]" value= "10"  /> Senior 10% <br/>
<input type="checkbox" name="korting[]" value= "5" /> klant 5% <br/> 

和这样的处理:

<?php
$korting = 0;
if (isset($_POST['korting']) && is_array($_POST['korting'])) {
  $korting = array_sum($_POST['korting']);
}
echo 'Korting is ' . $korting . '%';
?>
于 2013-01-20T14:33:07.410 回答