Luke Stagg
Luke Stagg

Reputation: 1

Jquery is stopping form from submitting

I created a form submission with Bootstrap 4 & PHPMailer which works perfectly until i add some jQuery to try to display an alert box to show success or failure.

`

                         <div id="messages" class="hide" role="alert">
          <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
          <div id="messages_content"></div>
        </div>


                        <form id="form" role="form" action="test.php" method="post" enctype="multipart/form-data" class="form">
                            <div class="row">
                                <div class="col-md-3">
                                    <label class="input-group g-rounded-3 g-mb-0--md">
                                        <input class="form-control g-brd-white g-brd-right-none border border-secondary border-right-0 u-has-success-v1-2" type="text" placeholder="Your name" name="name" required>
                                        <div class="input-group-append">
                                            <span class="input-group-text g-bg-white g-color-gray border border-secondary border-left-0"><i class="fa fa-user"></i></span>
                                        </div>
                                    </label>
                                </div>
                                <div class="col-md-3">
                                    <label class="input-group g-rounded-3 g-mb-0--md">
                                        <input class="form-control g-brd-white g-brd-right-none border border-secondary border-right-0" type="tel" placeholder="Your phone number" name="number" required>
                                        <div class="input-group-append">
                                            <span class="input-group-text g-bg-white g-color-gray border border-secondary border-left-0"><i class="fa fa-phone"></i></span>
                                        </div>
                                    </label>
                                </div>
                                <div class="col-md-4">
                                    <select class="custom-select w-100 mr-sm-3 mb-3 mb-lg-0 border border-secondary" id="inlineFormCustomSelect" name="service">
                                        <option selected="">Choose Services...</option>
                                        <option value="General Enquiry">General Enquiry</option>
                                        <option value="Light Demolition">Light Demolition</option>
                                        <option value="Rubbish Clearance">Rubbish Clearance</option>
                                        <option value="House Clearance">House Clearance</option>
                                    </select>
                                </div>
                                <div class="col-md-2">
                                    <button class="btn btn-md u-btn-white text-uppercase g-font-size-12 g-font-weight-600 g-rounded-5 border border-secondary bg-white btn-block" type="submit">Request Call Back</button>
                                </div>
                            </div>
                        </form>

                  </section>


                </div>
            </div>
        </section>



        <script src="http://code.jquery.com/jquery.js"></script>
    <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>

    <script>
        $('#form').submit(function(e) {
            $('#messages').removeClass('hide').addClass('alert alert-success alert-dismissible').slideDown().show();
            $('#messages_content').html('<h4>MESSAGE HERE</h4>');
            $('#modal').modal('show');
            e.preventDefault();
        });
    </script>`

Once the form is submitted, the alert box is displayed with the text field remaining populated (clearly not being sent anywhere) and no email being received.

If i remove the jQuery, the form is sent to the php file, processed and returned back the the page and email is received. i cant seem to find this specific problem, i cant see the problem!

Upvotes: 0

Views: 73

Answers (1)

Jack Bashford
Jack Bashford

Reputation: 44125

The problem is in the bottom of your jQuery:

e.preventDefault(); 

This prevents the default action of the event of the form submission, so the form won't submit. Remove this line, and it will work.

Upvotes: 2

Related Questions