-3

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

William
  • 1
  • 1
  • While it is legal to define funtions inside an IF it is dangerous to do it. If you dont enter the IF the function wont exists. Instead define the functions at the top of the script – RiggsFolly Sep 14 '22 at 16:17
  • PS Most of us will not follow links from strangers, so instead paste code or error messages into the question as text – RiggsFolly Sep 14 '22 at 16:21
  • 1
    That's a terrible example of code and you shouldn't try to learn from it. That said, it does do what it is supposed to do. I've tested it and I cannot reproduce the problem you are having. My best guess is that [this is a duplicate](https://stackoverflow.com/questions/5121495/php-code-is-not-being-executed-but-the-code-shows-in-the-browser-source-code?answertab=votes#tab-top). – Quentin Sep 14 '22 at 16:22
  • 1
    `$_POST["course_submit_btn"]` You dont have a field with the name `course_submit_btn` Please try and make your questions make some sense – RiggsFolly Sep 14 '22 at 16:24
  • 1
    Welcome, to improve your experience on SO please [take the tour](http://stackoverflow.com/tour) and read [how to ask](https://stackoverflow.com/help/how-to-ask), an [On Topic question](https://stackoverflow.com/help/on-topic), then look at the [Question Check list](https://meta.stackoverflow.com/questions/260648/stack-overflow-question-checklist), the [perfect question](http://codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question/) and how to create a [Minimal, Complete and Verifiable Example](http://stackoverflow.com/help/mcve) – RiggsFolly Sep 14 '22 at 16:24
  • @RiggsFolly of course I have. Did you see the file on link? The issue is that the if is executed for the example code (just a example) and not for the form
    .
    – William Sep 15 '22 at 16:54
  • Just for your edification, I have enough reps to allow me to see the edits you have made :) – RiggsFolly Sep 15 '22 at 16:58
  • Oh and the answer is NO, as all I get from the link is `There was an error retrieving your file.` – RiggsFolly Sep 15 '22 at 17:00
  • PS Most of us wont follow links posted by strangers, for obvious reasons, and it is suggested by the site that you do not post links to off site resources for that very reason amongst others – RiggsFolly Sep 15 '22 at 17:01
  • for a moment I thought I wrote the wrong field, but I went back. If you didn't look at the code in the link (which is too long to post here) I don't even know why you bothered to reply. – William Sep 15 '22 at 18:01
  • Code to long to post? Then read the requirement for [Minimal, Complete and Verifiable Example](http://stackoverflow.com/help/mcve) – RiggsFolly Sep 16 '22 at 07:03
  • in fact to improve your experience on SO please [take the tour](http://stackoverflow.com/tour) and read [how to ask](https://stackoverflow.com/help/how-to-ask), an [On Topic question](https://stackoverflow.com/help/on-topic), then look at the [Question Check list](https://meta.stackoverflow.com/questions/260648/stack-overflow-question-checklist), the [perfect question](http://codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question/) and how to create a [**Minimal**, Complete and Verifiable Example](http://stackoverflow.com/help/mcve) – RiggsFolly Sep 16 '22 at 07:05

1 Answers1

2
<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
PiotrG
  • 31
  • 5