简体   繁体   中英

One form with two submit buttons and different actions for each button

I have spent several hours trying to find a solution to my issue, but just can't seem to find the proper solution. Thanks in advance for your assistance!

I have ONE html form with:

<form id="columnarForm"  action="formindb_hoh_1.php" enctype="multipart/form-data" method="post" />

I would like to have TWO submit buttons:

<input type="image" name="camper" value="camper" src="../images/apps/camperBtn.png" class="submit_button" />
<input type="image" name="medical" value="medical" src="../images/apps/medicalBtn.png"class="submit_button" />

I would like the first submit button to have the action of formindb_hoh_1.php when clicked and the second submit button to have the action of formindb_hoh_2.php, but am unsure how to make one button have one action and have the other button has a different action.

No JS, use HTML5.

Change the submit buttons to use new HMTL5 button attribute formaction :

<button type="submit" name="camper" formaction="formindb_hoh_1.php">Camper</button>
<button type="submit" name="camper" formaction="formindb_hoh_2.php">Medical</button>

Reference: http://devdocs.io/html/element/button

Refer this :

Multiple submit buttons php different actions

Put this script in your page :

<script>
    function submitForm(action)
    {
        document.getElementById('columnarForm').action = action;
        document.getElementById('columnarForm').submit();
    }
</script>

Modify your input code :

<input type="image" name="camper" onclick="submitForm('formindb_hoh_1.php')" value="camper" src="../images/apps/camperBtn.png" class="submit_button" />
<input type="image" name="medical" onclick="submitForm('formindb_hoh_2.php')" value="medical" src="../images/apps/medicalBtn.png"class="submit_button" />

Finally, I got my answer after spending more than an hour. I tried a various method like switch case, jquery, and JavaScript. But I only used HTML5 formaction tag and now my form two submit button working at two location.

Code :

 <input type="submit" class="btn btn-success" value="Pay Now" name="submit" style="width: 100%; letter-spacing: 1px; color: #fff; border: none;padding: 10px;" formaction="example.php"> <button type="submit" class="btn btn-success" name="submit" style="width: 100%; letter-spacing: 1px; color: #fff; border: none;padding: 10px; margin-top: 15px;" formaction="mail.php"> Button</buttton>

I have the same issue. but the thing is the first button function is in JS while the other is using PHP.

$('#add-bookingid').submit(function(e){
e.preventDefault();
calculateDistance();
});

You see $('#add-bookingid').submit so both buttons have same button type as submit.

Main Purpose

Button# 1

Button# 2

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