1

I have multiple forms in multiple pages, I send them to the same page called handler that handles the form inputs, I currently have multiple if statements to check what form was submitted, how can I create a class in the handler page and make each form submit to a certain function in the class so it doesn't go through thee if statements looking for which form the user submits, here's how my current code looks like.

The forms:

<form action="handler.php" method="post">
   // Form elements..
   <button type="submit" name ="finishfirst" >finish first</button>     
</form>

<form action="handler.php" method="post">
   // Form elements..
   <button type="submit" name ="finishsecond" >finish second</button>     
</form>

// Another forms..

The handler page:

if (isset($_POST['finishfirst'])) {
   // Irrelevant code..
}

if (isset($_POST['finishsecond'])) {
   // Irrelevant code..
}

// Another if statements..
İsmail Y.
  • 3,579
  • 5
  • 21
  • 29
H NASSEREL
  • 43
  • 5
  • 1
    _“make each form submit to a certain function in the class”_ - you can’t submit a form “to” a function. You will still need code in some place, that decides _which_ method to call, based on the submitted parameters. – CBroe Aug 03 '21 at 09:57
  • @CBroe what's a better approach than the if statements? what can i do to improve my code? – H NASSEREL Aug 03 '21 at 10:08
  • 1
    You could use a [`switch`](https://www.php.net/manual/en/control-structures.switch.php) which is more "readable" than a long nested if - elseif. Just give the buttons all the same name and use the `value` attribute to determine the action - [example](https://stackoverflow.com/a/38781638/446594) – DarkBee Aug 03 '21 at 10:10

0 Answers0