tags:

views:

120

answers:

2

Hello,

I am trying to perform some calculations with a form but every time i try to work with checkboxes it goes wrong.

The checkboxes are beign set on value 1 in the form itselff and are being checked if there checked or not.

$verdieping = isset($_POST["verdieping"]) ? $_POST["verdieping"] : 0;  
$telefoon = isset($_POST["telefoon"]) ? $_POST["telefoon"] : 0;  
$netwerk = isset($_POST["netwerk"]) ? $_POST["netwerk"] : 0; 

When i try to do calculations every works expect for the options with the checkboxes.
When both checkboxes (telefoon & netwerk) are selected the value should be 30.
If only one is selected the value should be 20.
But no mather what i have tried to write down it always give problem, and it always uses 20, never the value 30.

How do i solve this problem? Or suppose i am writing the syntax all wrong to lay conditions to a calculation? Any input appreciated.

$standnaam = $_SESSION["standnaam"];
$oppervlakte = $_SESSION["oppervlakte"];
$verdieping = $_SESSION["verdieping"];
$telefoon = $_SESSION["telefoon"];
$netwerk = $_SESSION["netwerk"];


if ($oppervlakte <= 10)
$tarief = 100;

if ($oppervlakte > 10 && $oppervlakte <= 20)
$tarief = 90;

if ($oppervlakte > 20)
$tarief = 80;


if($verdieping == 1)
{
$prijsVerdieping = $oppervlakte * 120;
}
else
{
$prijsVerdieping = 0;
}

if(($telefoon == 1) && ($netwerk == 1))
{
$prijsCom = 30; // never get this value, it always uses 20
}

if(($telefoon == 1) || ($netwerk == 1))
{
$prijsCom = 20;
}

$prijsOpp = $tarief * $oppervlakte; // works
$totalePrijs = $prijsOpp + $prijsVerdieping + $prijsCom; //prijsCom value is always wrong

Regards.

EDIT: full code below in 2 php files

<?php
if (!empty($_POST))
{ 
$standnaam = $_POST["standnaam"];
$oppervlakte = $_POST["oppervlakte"];
//value in the form van checkboxes op 1 zetten!
$verdieping = isset($_POST["verdieping"]) ? $_POST["verdieping"] : 0;  //if checkbox checked     value 1 anders 0
$telefoon = isset($_POST["telefoon"]) ? $_POST["telefoon"] : 0;  
$netwerk = isset($_POST["netwerk"]) ? $_POST["netwerk"] : 0; 


if (is_numeric($oppervlakte)) 
{
    $_SESSION["standnaam"]=$standnaam;
    $_SESSION["oppervlakte"]=$oppervlakte;
    $_SESSION["verdieping"]=$verdieping;
    $_SESSION["telefoon"]=$telefoon;
    $_SESSION["netwerk"]=$netwerk;
    header("Location:ExpoOverzicht.php"); //verzenden naar ExpoOverzicht.php
}
else 
{
    echo "<h1>Foute gegevens, Opnieuw invullen a.u.b</h1>";
}  
}

?>

<form action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="post" id="form1">
<h1>Vul de gegevens in</h1>
<table>
    <tr>
        <td>Standnaam:</td>
        <td><input type="text" name="standnaam" size="18"/></td>
    </tr>
    <tr>
        <td>Oppervlakte (in m^2):</td>
        <td><input type="text" name="oppervlakte" size="6"/></td>
    </tr>
    <tr>
        <td>Verdieping:</td>
        <td><input type="checkbox" name="verdieping" value="1"/></td>
        <!--value op 1 zetten voor checkbox! indien checked is value 1 -->
    </tr>
    <tr>
        <td>Telefoon:</td>
        <td><input type="checkbox" name="telefoon" value="1"/></td>
    </tr>
    <tr>
        <td>Netwerk:</td>
        <td><input type="checkbox" name="netwerk" value="1"/></td>
    </tr>
    <tr>
        <td><input type="submit" name="verzenden" value="Verzenden"/></td>
    </tr>
</table>

2nd page with calculations:

<?php

$standnaam = $_SESSION["standnaam"];
$oppervlakte = $_SESSION["oppervlakte"];
$verdieping = $_SESSION["verdieping"];
$telefoon = $_SESSION["telefoon"];
$netwerk = $_SESSION["netwerk"];


if ($oppervlakte <= 10)
$tarief = 100;

if ($oppervlakte > 10 && $oppervlakte <= 20)
$tarief = 90;

if ($oppervlakte > 20)
$tarief = 80;


if($verdieping == 1)
{
$prijsVerdieping = $oppervlakte * 120;
}
else
{
$prijsVerdieping = 0;
}

if(($telefoon == 1) && ($netwerk == 1))
{
$prijsCom = 30;
}

 if(($telefoon == 1) || ($netwerk == 1))
{
$prijsCom = 20;
}

$prijsOpp = $tarief * $oppervlakte; // werkt
$totalePrijs = $prijsOpp + $prijsVerdieping + $prijsCom; 

echo "<table class=\"tableExpo\">";

echo "<th>Standnaam</th>";
echo "<th>Oppervlakte</th>";
echo "<th>Verdieping</th>";
echo "<th>Telefoon</th>";
echo "<th>Netwerk</th>";
echo "<th>Totale prijs</th>";

    echo "<tr>";

        echo "<td>$standnaam</td>";
        echo "<td>$oppervlakte</td>";
        echo "<td>$verdieping</td>";
        echo "<td>$telefoon</td>";
        echo "<td>$netwerk</td>";
        echo "<td>$totalePrijs</td>";

    echo "</tr>";

echo "</table>";

?>

<a href="ExpoFormulier.php">Terug naar het formulier</a>

</body>
</html>
+4  A: 

One problem I've noticed are these lines,

if(($telefoon == 1) && ($netwerk == 1))  {
$prijsCom = 30; // will get set to 30.
}

if(($telefoon == 1) || ($netwerk == 1))  {
$prijsCom = 20; // will now be set to value of 20.
}

Here is why, if $telefoon and $netwerk are both 1, $prijsCom is set to the value of 30. It leaves that if block and goes down onto the next one, i.e.

if(($telefoon == 1) || ($netwerk == 1))  {
    $prijsCom = 20;
}

It will evaluate to true since $telefoon == 1 evaluates to true and will override the value of $prijsCom to be 20.

Depending on how the code will be used, as a possible work-around, you could add the || condition first, so the value is set to 20 whether $telefoon or $netwerk is set to 1 and then check to see if they both are 1.

Update:

When looking at your code, I notice you are using $_SESSION variables, but you have not called session_start() at the beginning of the file,

<?php
session_start(); // <--you need to call this first
$standnaam = $_SESSION["standnaam"];
$oppervlakte = $_SESSION["oppervlakte"];
...

This may or may not be where your other problem lies, but whenever you use $_SESSION you need to call session_start first.

Anthony Forloney
What would be better way of assign values to a checkbox then? To know what checkbox is selected and perform calculations with if selected. Cheers!
Sef
You could put an array as the `name` attribute of your checkbox, i.e. `name[]`, and then if you call `$var = $_POST['name']`, `$var` will be an array of all the selected checkboxes. Now that I think of it, it will contain only the `value` attributes inside the array, which in your case would be all integers, I am not sure if thats what you wanted. It is tough to give a better example because of the language barrier, if you need me to clarify let me know.
Anthony Forloney
Heya, yeah lets keep it at that. Your solution above fixed the problem but i seem i have similar problems over my code. If i select everything i get right results. If i select everything except the floor i get correct resulst. Anything of combinations in between gives wrong results. Am i overriding some calculations again?
Sef
Can you give me an example so I can look through your code?
Anthony Forloney
Sef
It's tough for me to actually help as to what is going on, I have no idea where those numbers are coming from.
Anthony Forloney
Just from filling the form in. In the form actions are the things listed i fill in. A name, a oppervlakte (for example 5m²) and 3 checkboxes.Anyway your right i better leave it at this then.Thanks again!
Sef
Not a problem, good luck.
Anthony Forloney
A: 

Call session_start(); after <?php.

Jan Tojnar
I think we can assume he has done that in the beginning.
ggfan
His source doesn't contain it. I've tried it and it worked with started session.
Jan Tojnar