Home>

Perform count-up and count-down by clicking in php
I'm writing a process to calculate the total
I would like to have a professor because I get an error.
Also, I would like to display $Total on a separate page
Does not work with require.

<? php
$cut = 0;
if ($_ POST ['cut']) {
    $cut = $_POST ['cut'];
}
if ($_POST ['cutplus']) {
    $cut ++;
} elseif ($_POST ['cutminus']) {
    $cut--;
}
$color = 0;
if ($_ POST ['color']) {
    $color = $_POST ['color'];
}
if ($_POST ['colorplus']) {
    $color ++;
} elseif ($_POST ['colorminus']) {
    $color--;
}
$total = 0;
$total = $cut + $color;

?>
<? = $total?>
 <? = $cut?>
 <? = $color?>
 <form action = "" method = "POST">
     <input type = "hidden" name = "cut" value = "<? = $cut?>">
     <input type = "submit" name = "cutplus" value = "reception">
 </form>
<form action = "" method = "POST">
     <input type = "hidden" name = "cut" value = "<? = $cut?>">
     <input type = "submit" name = "cutminus" value = "Information">
 </form>
 <br>
 <form action = "" method = "POST">
     <input type = "hidden" name = "color" value = "<? = $color?>">
     <input type = "submit" name = "colorplus" value = "reception">
 </form>
<form action = "" method = "POST">
     <input type = "hidden" name = "color" value = "<? = $color?>">
     <input type = "submit" name = "colorminus" value = "Information">
 </form>

Error

Notice: Undefined index: cut in C: \ xampp \ htdocs \ gomi \ newfile.php on line 4

Notice: Undefined index: cutplus in C: \ xampp \ htdocs \ gomi \ newfile.php on line 8

Notice: Undefined index: cutminus in C: \ xampp \ htdocs \ gomi \ newfile.php on line 10

Notice: Undefined index: color in C: \ xampp \ htdocs \ gomi \ newfile.php on line 17

Notice: Undefined index: colorplus in C: \ xampp \ htdocs \ gomi \ newfile.php on line 21

Notice: Undefined index: colorminus in C: \ xampp \ htdocs \ gomi \ newfile.php on line 23

php
  • Answer # 1

    $_ POST ['cut'], and when referring to a variable that does not know whether it exists or not
    Normally uses isset ().

    For example:

    if (isset ($_ POST ['cut'])) {// Check if cut parameter is received
        if (is_numeric ($_ POST ['cut'])) {// Check if the cut parameter is numeric
            $cut = $_POST ['cut'];
        }
    }

    The equivalent behavior can be written on a single line using filter_input ().

    $cut = filter_input (INPUT_POST, 'cut', FILTER_VALIDATE_INT);

    From that point of view, check with isset () and refer to it.
    And be careful to verify that the received parameters are not unexpected.