PHP Form $_POST['button'] always false

Viewed 37

I'm trying to get submit form event to update a field. I found a test code on internet and that's worked. But when I change the button name for my button nothing happens, and the if always returns false to my button and I have no idea what i'm doing wrong.

Example code works:

<form action="" method="POST">
    <input type="submit" name="add" Value="Call Add fun">
    <input type="submit" name="sub" Value="Call Sub funn">
<?php echo @$a; ?>
<?php
if(@$_POST['add'])
{
    function add()
    {
        $a="You clicked on add fun";
        echo $a;
    }
    add();
} else if (@$_POST['sub']) {
    function sub()  
    {
        $a="You clicked on sub funn";
        echo $a;  
    }
    sub();  
}
?>

I've tried some variations with if (isset($_POST["course_submit_btn"])) but again nothin happens.

I don't know if it has any interface, but the page url is "panel/create-course/?course_ID=3493/"

My page https://www.file.io/c0pn/download/C1kmP2TblfcU

1 Answers
<form action="" method="POST">
    <input type="submit" name="add" Value="Call Add fun">
    <input type="submit" name="sub" Value="Call Sub fun">

<?php
    if(isset($_POST['add'])) {
        add();
    } else if (isset($_POST['sub'])) {
        sub();
    }

    function add() {
        echo "You clicked on add fun";
        // todo: more meaningful functionality
    }

    function sub() {
        echo "You clicked on sub fun";
        // todo: more meaningful functionality
    }
?>

Your code should work, however there are some tips, you should stick to:

  1. Use consistent code styling (spaces, brackets etc),
  2. Use more meaningful names, in a few days you won't know what variable $a is for
  3. Don't put functions within if sections
Related