I have a simple form in HTML that contains two buttons. Button 1 which action in the form tag submits it to another php page e.g. button1-action.php which submits data to a third party API and Button 2 which I want to submit to the same page if it is clicked without going to button1-action.php. In its simplest method the form is as follows:
<?php echo '<form name="form123" id="form123" action="button1-action.php" method="POST">'; echo '<input type="text" name="first_name" id="first_name></input>'; echo '<button name="button1" id="button1" value="button1">Button 1</button>'; echo '<button name="button2" id="button2" value="button2">Button 2</button>'; echo '</form>';
?> This is what I tried so far
$action = null; if (isset($_POST['button1'])) { $action = 'button1-action.php'; } elseif (isset($_POST['button2'])) { $action = $_SERVER["PHP_SELF"]; } echo '<form name="form123" id="form123" action="' . $action . '" method="POST">'; echo '<input type="text" name="first_name" id="first_name></input>'; echo '<button name=" button1" id="button1" value="button1">Button 1</button>'; echo '<button name="button2" id="button2" value="button2">Button 2</button>'; echo '</form>';
However, it doesn't seem to be working. I tried to look for solutions but I haven't been successful.
I'm interested in any solution, but I would prefer solving it using PHP and not JavaScript.