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

?>
Daniel Andrei Mincă
  • 4,446
  • 2
  • 19
  • 30

3 Answers3

1

Try this:

<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";    
    }


?>
Nil'z
  • 7,487
  • 1
  • 18
  • 28
0

You have to tell the target page for the form:

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

And also your php code will execute itself BEFORE and AFTER you send the form.

If you want the php code execute itself only AFTER you send the form, use

if(isset($_GET['salarBrut'])){ 
    // your code 
}
Lan
  • 709
  • 1
  • 8
  • 16
  • 2
    and remember to [sanitize](http://stackoverflow.com/questions/129677/whats-the-best-method-for-sanitizing-user-input-with-php?lq=1) your request! – Lan Sep 06 '13 at 15:32
  • If I'm editing the PHP in the HTML I don't have to tell the target where to get it, you just have to leave it blank. It's the standards definition. – Daniel Andrei Mincă Sep 06 '13 at 20:31
0

Your input has an error in it. You need it be

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

Note the name= bit.

Also, I would probably POST the form and set it's action to be the current page:

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

Should work.

adampetrie
  • 1,130
  • 1
  • 11
  • 23