dongshuofu0039 2010-04-16 13:40
浏览 7
已采纳

PHP表单复选框计算

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>
  • 写回答

2条回答 默认 最新

  • dongtangu6144 2010-04-16 13:47
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥50 我撰写的python爬虫爬不了 要爬的网址有反爬机制
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等