Search code examples
javascripthtmlformspreventdefault

I can't get my form to submit after using preventdefault


I have a form inside a modal structure where I recollect some info from the user, after that I want to submit that info into a post request so I can process it in python.

The problem is that I need to check first if the user actually put the info, so I won't get empty values.

I'm using .preventDefault() in order to stop the submission and check the values. The program posts fine as long as I don't use the preventDefault, and actually doesn't post when using it.
The problem is that after passing all the conditions, it should post, but is not doing it and I don't know why.

Here is the code of the form:

<form id="forminout" action="/in_out" method="post">
        <div class="modal fade bd-example-modal-lg" tabindex="-1" role="dialog" id="incomeModal">
            <div class="modal-dialog modal-lg" role="document">
                <div class="modal-content">
                    <div class="modal-header">
                        <h5 class="modal-title">Add your new income</h5>
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                             <span aria-hidden="true">&times;</span>
                        </button>
                    </div>
                    <div class="modal-body" id="new_incomes">
                        <div style="padding:3%">
                            <input step=".01" type="number" class="form-control input-sm" name="incoming" min="0" placeholder="0">
                            <select class="incomselect" name="incomeselects" style="height:30px;">
                                <option selected disabled>This income is coming from?</option>
                                <option value="rent">Rents</option>
                                <option value="salary">Salary</option>
                                <option value="invest_reveneu">Investments reveneus</option>
                                <option value="others">Others</option>
                            </select>
                            <select class="incomdate" name="incomedate" style="height: 30px;">
                                <option selected disabled>Incoming date</option>
                                <option value=5>1 - 5</option>
                                <option value=10>6 - 10</option>
                                <option value=15>11 - 15</option>
                                <option value=20>16 - 20</option>
                                <option value=25>21 - 25</option>
                                <option value=31>26 - 31</option>
                            </select>
                        </div>
                    </div>
                    <div class="modal-footer">
                        <button class="btn btn-primary" id="addinbutt" type="submit">Save changes</button>
                        <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                    </div>
                </div>
            </div>
        </div>
   </form>

Which is working fine because the post is actually happening.

And here is the "simplified" code of the post request:

$("document").ready(function(){
    $("form").on("submit", function(e){
        errors = 0;
        e.preventDefault();
        if (errors==0)
        {
            document.getElementById("#forminout").submit();
        }
    })
})

In this case the errors==0 condition is only used as a simplification, supposing that all the actual conditions I need to check are ok.

I just can't get it to post.


Solution

  • solution : write this.submit() or e.target.submit() instead of doc.getElById().submit()


    $("document").ready(function(){
        $("#forminout").on("submit", function(e){
            e.preventDefault();
            // ur conditions ...
            this.submit();
        })
    })
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <form id="forminout" action="/in_out" method="post">
            <div class="modal fade bd-example-modal-lg" tabindex="-1" role="dialog" id="incomeModal">
                <div class="modal-dialog modal-lg" role="document">
                    <div class="modal-content">
                        <div class="modal-header">
                            <h5 class="modal-title">Add your new income</h5>
                            <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                                 <span aria-hidden="true">&times;</span>
                            </button>
                        </div>
                        <div class="modal-body" id="new_incomes">
                            <div style="padding:3%">
                                <input step=".01" type="number" class="form-control input-sm" name="incoming" min="0" placeholder="0">
                                <select class="incomselect" name="incomeselects" style="height:30px;">
                                    <option selected disabled>This income is coming from?</option>
                                    <option value="rent">Rents</option>
                                    <option value="salary">Salary</option>
                                    <option value="invest_reveneu">Investments reveneus</option>
                                    <option value="others">Others</option>
                                </select>
                                <select class="incomdate" name="incomedate" style="height: 30px;">
                                    <option selected disabled>Incoming date</option>
                                    <option value=5>1 - 5</option>
                                    <option value=10>6 - 10</option>
                                    <option value=15>11 - 15</option>
                                    <option value=20>16 - 20</option>
                                    <option value=25>21 - 25</option>
                                    <option value=31>26 - 31</option>
                                </select>
                            </div>
                        </div>
                        <div class="modal-footer">
                            <button class="btn btn-primary" id="addinbutt" type="submit">Save changes</button>
                            <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                        </div>
                    </div>
                </div>
            </div>
       </form>

    EDIT :

    you can even write this based on e parameter :

    $("document").ready(function(){
            $("#forminout").on("submit", function(e){
                e.preventDefault();
                // ur conditions ...
                e.target.submit();
            })
        })