<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>How to Create Two Submit Buttons with Different Action in One Form</title> <link rel="stylesheet" type="text/css" href="bootstrap/css/bootstrap.min.css"> </head> <body> <div class="container"> <h1 class="page-header text-center">Two Submit Buttons with Different Action</h1> <div class="row"> <div class="col-sm-3 col-sm-offset-2"> <h3>Submit Form</h3> <form method="POST" id="myForm"> <div class="form-group"> <label for="firstname">Firstname</label> <input type="text" class="form-control" id="firstname" name="firstname"> </div> <button type="submit" class="btn btn-primary" onclick="submitForm('page1.php')">Submit1</button> <button type="submit" class="btn btn-primary" onclick="submitForm('page2.php')">Submit2</button> </form> </div> <div class="col-sm-5"> <table class="table table-bordered"> <thead> <th>Firstname</th> <th>Submitted Via</th> </thead> <tbody> <?php } foreach($_SESSION['names'] as $row){ echo " <tr> <td>".$row['firstname']."</td> <td>".$row['sub_via']."</td> </tr> "; } ?> </tbody> </table> </div> </div> </div> <script type="text/javascript"> function submitForm(action) { var form = document.getElementById('myForm'); form.action = action; form.submit(); } </script> </body> </html>
<?php $data['firstname'] = $_POST['firstname']; $data['sub_via'] = 'Submit1'; ?>
<?php $data['firstname'] = $_POST['firstname']; $data['sub_via'] = 'Submit2'; ?>