phphtmlformsform-submit

Ignore required inputs in a form if a button (input type of submit) is clicked


I have two buttons for one form (two input types of submit).

One button requires more inputs than the other button.

How can I press the button that doesn't require certain inputs without being stopped because it indicates that there are required inputs that are empty (although they are actually only required for the other button)?

I wrote a small piece of code that reflects my question. Let's assume that when pressing the firstButton, you require all inputs. When you press the secondButton you require only the second input. How would I ignore the first required input in that case?

<form id="formID" action="processForm.php" method="post">
    <input id="firstInput" name="input1" placeholder="Amount" required>
    <input id="secondInput" name="input2" placeholder="Amount2" required>
    <input type="submit" value="firstInput" alt="firstButton">
    <input type="submit" value="secondInput" alt="secondButton">
</form>

Thank you, any help is much appreciated! Al


Solution

  • Ok so I was able to do it with the following code:

    <form id="formID" action="processForm.php" method="post">
        <input id="firstInput" name="input1" placeholder="Amount" required>
        <input id="secondInput" name="input2" placeholder="Amount2" required>
        <input id="firstButtonClick" type="submit" value="firstInput" alt="firstButton">
        <input id="secondButtonClick" type="submit" value="secondInput" alt="secondButton">
    </form>
    
    $(document).ready(function () {
        $("#secondButtonClick").click(function () {
            document.getElementById("secondInput").required = false;
        });
    });