Mike
Mike

Reputation: 55

Is it a correct approach in php?

first of all this is my first post in stack <3 and of course sorry for my bad English. So i'm studying PHP and i have something like a problem in my mind:

when i write a code like:

if ($_POST['submit'])
{
    $total = $_POST["total"];
    $var = $_POST["var"];

function func($total, $var)
    {
     $lost = $total * $var / 100;
     $income = $total - $income;
     $result = "Income - " . $income . "<br />Lost - " . $lost;

     return $result;
    }

echo func($total, $var);
}
else
{
?>


<?php 
// HTML FORM : i write to number example total = 1000
// and var = 200 and result is 1000 - (1000/100*20)
?>
<form method="POST" action="index.php">
    <input type="text" name="total" />
    <input type="text" name="var" />

    <input type="submit" name="submit" value="Submit" />
</form>
<?php }?>

there is an error: Notice: Undefined index: submit when i search in google, i found a something like solution, error_reporting (E_ALL ^ E_NOTICE) but i think this is not a correct solution.

Upvotes: 1

Views: 108

Answers (2)

Frank Farmer
Frank Farmer

Reputation: 39356

empty() and isset() are the tools you want in this situation

if (!empty($_POST['submit'])) {

Upvotes: 2

Brian Patterson
Brian Patterson

Reputation: 1625

I think its telling you that that $_POST['submit'] is not set ...

Try using this instead ..

if (array_key_exists('submit', $_POST)) {

Upvotes: 2

Related Questions