简体   繁体   中英

Post 2 different form actions with 2 submit buttons

I have a table where I use an UPDATE button with action form to update the data. But I also need to submit the table using another form action and submit button.

Here's my button:

<button class="btn btn-info" type="submit" name="update" value="update">Update Table</button>
<button class="btn btn-success" type="submit" name="submit_req" value="submit_req">Submit Request</button>

This is currently my form action for the table:

<form method="post" action="">

And this is my PHP if else statement. The first submit_req is supposed to POST to another URL. But I don't know how to do it. I already tried header, but it won't work.

<?php
    if(isset($_POST['update'])){
        if(!empty($_SESSION['cart'])){
        foreach($_POST['quantity'] as $key => $val){
            if($val==0){
                unset($_SESSION['cart'][$key]);
            }else{
                $_SESSION['cart'][$key]['quantity']=$val;
            }
        }
        }
    }elseif (isset($_POST['submit_req'])) {
        //form action: insert_order.php
    }
?>

You can use a JS function to redirect to another page for the submit button Here page.php is where you want to send your form to. formID is the id of the form.

<button class="btn btn-success" type="submit" name="submit_req" value="submit_req" onclick="submitForm('page.php')">Submit Request</button>

<script type="text/javascript">
  function submitForm(action)
  {
    document.getElementById('formID').action = action;
    document.getElementById('formID').submit();
  }
</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM