vendredi 4 décembre 2015

how to add up values from checkboxes in php

for training purposes i've made a little code which outputs the value of a checkbox. this works well, but since i am able to check multiple checkboxes i want them to add up. this is my code;

<table border=0 cellpadding=0 cellspacing=0 width=100%>
<form name="orderform" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">


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

To output the value ive written;

<?php
if(isset($_POST["korting"]))
{
if($_POST["korting"]==15)
{
    echo ("15 procent korting");
}
else if ($_POST["korting"]==10)
{
    echo ("10 procent korting");
}
else if($_POST["korting"]==5)
{
    echo ("5 procent korting");
}
else if(isset($_POST["korting"]) && (isset($_POST["korting"])))
{
    if($_POST["korting"]==25)
{
    echo ("25 procent korting");
}
}
}


?>

As long as one checkbox is checked, everything works fine. as soon as is check more than one it only uses the last one. I've tried multiple thing like:

else if(isset($_POST["korting"]) && (isset($_POST["korting"]))
{
    echo ("25 procent korting");
}

and

else if($_POST["korting"=15] && $_POST["korting"]=10)
{
    echo ("25 procent korting");
}

both do not give an error on the page but also don't work. I know it's probably better to use completely different approach but for now this is how the book teaches me

Greetings,

Lennart

Aucun commentaire:

Enregistrer un commentaire