0

I created a form in which I want to enter a gross salary ($salBrut) and when I press the "Calculate" button, it shows me the $salNet, which is the NET salary.

So far the form automatically has nothing in the input text, and it gives me -299 result directly, without letting me calculate the desired salary! And even if I enter any value, it still won't read it...

What should I do in order to make it recognise my value entered in the ? $contributii are the taxes

Code is below:

<form method="get" action="">
    <label>Introduceti salariul dvs. <u>brut</u></label>
    <br>
    <input type="text" name"salarBrut" value=""/>
    <br>
    <input type="submit" name="btn" value="Calculate!"/>
</form>



<?php

$salBrut = $_GET['salarBrut'];
$contributii = array("pensii" => 105,
            "sanatate" => 55,
            "somaj" => 5,
            "impozit" => 134);
$totContrib = array_sum($contributii);

$salNet = $salBrut-$totContrib;

echo "Salariul net este $salNet";

?>
4

3 回答 3

1

尝试这个:

<form method="get" action="">
    <label>Introduceti salariul dvs. <u>brut</u></label>
    <br>
    <input type="text" name="salarBrut" value="<?=(isset( $_GET['salarBrut'] ))?$_GET['salarBrut']:''?>"/>
    <br>
    <input type="submit" name="btn"/>
</form>



<?php
    if( isset( $_GET['salarBrut'] ) ){
        echo "sal :".$salBrut = $_GET['salarBrut'];
        $contributii = array("pensii" => 105,
                    "sanatate" => 55,
                    "somaj" => 5,
                    "impozit" => 134);
        echo "<br>sum :".$totContrib = array_sum($contributii);

        echo "<br>net :".$salNet = $salBrut-$totContrib;

        echo "<br>Salariul net este $salNet";    
    }


?>
于 2013-09-06T15:34:27.467 回答
0

您必须告诉表单的目标页面:

<form method="get" action="your_php_file.php">

而且您的 php 代码将在您发送表单之前之后自行执行。

如果您希望 php 代码仅在发送表单自行执行,请使用

if(isset($_GET['salarBrut'])){ 
    // your code 
}
于 2013-09-06T15:30:27.663 回答
0

您的输入有错误。你需要它

<input type="text" name="salarBrut" value=""/>

注意name=位。

另外,我可能会发布表单并将其操作设置为当前页面:

<?php

    $salBrut = $_POST['salarBrut'];

    $contributii = array("pensii" => 105,
            "sanatate" => 55,
            "somaj" => 5,
            "impozit" => 134);
    $totContrib = array_sum($contributii);

    $salNet = $salBrut-$totContrib;

    echo "Salariul net este $salNet";

?>

<form method="post" action="/">
    <label>Introduceti salariul dvs. <u>brut</u></label>
    <br>
    <input type="text" name="salarBrut" value=""/>
    <br>
    <input type="submit" name="btn" value="Calculate!"/>
</form>

应该管用。

于 2013-09-06T15:36:20.737 回答