开发者

PHP Forms checkbox calculation

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

The ch开发者_如何学Goeckboxes 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>


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.


Call session_start(); after <?php.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜