Search code examples
phpcheckboxself

cant get PHP_SELF to post checkbox


i was trying to get my check box to output "Korting is ... %" at the bottom of the page depending on which check box is checked e.g. 2th and 3th boxes are checked it would output "Korting is 15%" but i cant seem to get it to work. can someone please help me thank you in advance

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

Solution

    • Use radio instead of checkbox if you want one option to be selected
    • Do not use PHP_SELF, it is a security hole. Instead: <form action="">
    • Access the value with $_POST['korting']

    To sum up all values, make your form like this:

    <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/> 
    

    and the processing like that:

    <?php
    $korting = 0;
    if (isset($_POST['korting']) && is_array($_POST['korting'])) {
      $korting = array_sum($_POST['korting']);
    }
    echo 'Korting is ' . $korting . '%';
    ?>