The function fails to transmit any information

I'm having some trouble with my code. The ajax function isn't sending data and I can't figure out why. It might be related to the javascript function. Can someone please help me troubleshoot?

Interestingly, when I move the submit button outside of the form, it seems to work fine. Any idea why that might be?

<div class="modal fade" id="form-content" tabindex="-1" role="dialog" style="display: none; ">
    <div class="modal-dialog">
        <div class="modal-content">         
            <div class="modal-body">
                <div class="row">
                    <div class="col-md-12 text-center">
                        <div class="pop-up">                             
                            <div class="box-1">
                                <h3>Enter your email</h3>                                    
                            </div>
                            <img src="img/6.png" class="center-block" alt="..">
                            <div class="form-search space40">
                                <form class="contact">
                                    <div class="input-group input-group-lg mtop-10">
                                        <input type="text" name="Email" class="form-control input-lg" placeholder="email">
                                        <span class="input-group-btn">
                                            <button id="submit" class="btn btn-lg-sub model-btn" name="name">Submit</button>
                                        </span> 
                                    </div> 
                                </form>   
                            </div>
                            <div class="space30"></div>
                            <div>
                            </div>
                            <div class="space30"></div>
                        </div>
                    </div>
                </div>                       
            </div>                        
        </div>                  
    </div>
</div>
$(function() {
    $("button#submit").click(function(){
        $.ajax({
            type: "POST",
            url: "process.php",
            data: $('form.contact').serialize(),
            success: function(msg){
                $("#thanks").html(msg)
                $("#form-content").modal('hide'); 
            },
             error: function(){
                 alert("Oops! Something went wrong");
             }
        });
    });
});

Answer №1

If buttons are missing the type attribute, they are treated as submit buttons. Therefore, when clicking the button, you must prevent the default behavior which automatically submits the form. Alternatively, you can add type="button":

$(function() {
    $("button#submit").click(function(e) {
        e.preventDefault(); //This is necessary
        $.ajax({
            type: "POST",
            url: "process.php",
            data: $('form.contact').serialize(),
            success: function(msg) {
                $("#thanks").html(msg)
                $("#form-content").modal('hide');
            },
            error: function() {
                alert("failure");
            }
        });
    });
});

Alternatively:

<button id="submit" type="button" 
        class="btn btn-lg-sub model-btn" 
        name="name">submit</button>

I trust this information proves beneficial to you.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Encountering a bug: Error message pops up while trying to create an action

I have implemented a Jquery Jtable with functions for displaying data (list action), deleting records, and creating actions. While using it in asp.net web forms, I encountered an issue where the create action is not functioning correctly. The list and del ...

Interacting with jQuery mouse events on elements below the dragged image

I'm attempting to create a drag-and-drop feature for images using jQuery. While dragging, I generate a thumbnail image that follows the mouse cursor. However, this is causing issues with detecting mouseenter and mouseleave events on the drop target pa ...

Encountered a 404 Error when attempting to store data in mongoDb using node.js and express

Currently, I am facing an issue while trying to save data from Bootstrap input fields into my MongoDB database. Every time I attempt to do so, I encounter the error insertMovie:1 POST http://localhost:3000/insertMovie 404 (Not Found). Despite my efforts to ...

Troubleshooting Token Issue in Node.Js with Jest Testing (Encountering 401 "Unauthorized" Error)

Hey there, I am currently experimenting with Jest on a Node.js project where I need to test my functions. const userOne = { _id: userOneId, name: 'Mike', email: '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="fa9 ...

Using JavaScript, you can filter an array of objects based on a specific search input

I am in the process of implementing a filtering feature for a list using React, but surprisingly, I haven't been able to find any useful resources online to guide me through this common task. Currently, I have an array of users that I need to filter ...

Set up a Bootstrap date picker to populate two input boxes with a start and end date. Additionally, disable the ability for the date value to change

In my project, I have implemented Bootstrap Datepicker to set two input boxes for selecting start and end dates. The rule is that the start date must be after today and the end date must be after the selected start date. To disable specific dates in the da ...

Confirm information and send without reloading the page

I have been working on a form that requires validation and submission without refreshing the page. I managed to implement validation and submission, but it resulted in the page refreshing. Then, when I tried submitting without page refresh, the validation ...

Is there a way to prompt WebAPI to receive a complicated object as its argument for a DELETE HTTP request without relying on C# attributes?

Currently, my server is utilizing C#/WebAPI while the client side is using AngularJS. I am faced with a dilemma when it comes to formatting an HTTP DELETE request without specifying attributes in C#. Specifically, I am struggling with how to handle a meth ...

Even though a Jquery ajax error handler function is present, an exception is still being thrown

After submitting a form using ajax, the validation is carried out on the server side. If the validation fails, a json encoded object with the errors and status code 500 is returned. When jQuery receives this response on the client side, the error handler f ...

When attempting to upload a file using Form, it only allows for a single upload of that file and will not work when attempting to upload the same file again

After selecting a file from the file chooser and uploading it, I encounter an issue when attempting to submit the form with the same file again. The submission doesn't seem to trigger any action. If I select a file, upload it, then choose the same fi ...

Sharing data between child and parent components, and then passing it on to another child component in React Js

I have a scenario where I am passing props from a child component B to parent component A, and then from parent component A to child component C. Everything works fine when I pass the data from component B to A, but I encounter an issue when I try to set a ...

Icon from Bootstrap disappears when button text is modified

Recently, I made a modification to incorporate an icon into a button as shown below: Original Button Design However, once a user adds an item to the cart, the button transforms into Updated Button Appearance In my efforts to dynamically update the text ...

"Encountering an undefined object in AngularJS when trying to implement attribute two

Encountering an issue with a feature in AngularJS, the scenario is as follows: A controller and directive are defined like this: helpers.directive('someEvent', function() { ... scope: { api: '=' } ... contr ...

Converting a nested array of objects into an array of objects using async/await and two loops

Although this question may seem similar to others, my case is unique. I have already implemented a solution using a Synchronous loop, but now I want to switch to an Asynchronous loop. Unfortunately, I am struggling to make this transition. Synchronous loo ...

Encountering issues with running an AngularJS application (version 1.6) in Visual Studio following the activation of script

I am new to working on an angular 1.6 application and still learning the ropes. The project consists of an asp.net web api project that needs to be run before starting the angular project. Everything was running smoothly until I tried to debug a parameter ...

Populate a dropdown menu using Javascript

[Code] $.ajax ({ 'method': 'GET', 'source': '/bpv-registratie/periods/show_period_list_by_year.html', 'charge': function () { }, 'finish': function (xmlHttp) { ...

I'm having trouble finding the solution to setting a token in my request header

I have been following a tutorial in a book to build the authentication for my app. However, I am facing an issue where after logging in correctly, I am unable to set the token back into the request. The error message that I receive is: Failed to execute ...

Pressing the enter key to input password

I have searched through various resources for an answer to my problem, but none of them seem to address it in a way that helps. As a beginner in coding, I may need some extra guidance. Currently, I am only able to submit a password by clicking with the mo ...

What is the best way to retrieve the js window object within emscripten's EM_JS function?

I'm looking to access the window.location in an EM_JS method in order to call a JavaScript method from C++. My attempted approach was: EM_JS(const char*, getlocation, (), { let location = window.location; let length = lengthBytesUTF8(location ...

Javascript's second element does not trigger a click event with similar behavior

I'm currently facing an issue with displaying and hiding notification elements based on user interaction. My goal is to have multiple popup elements appear when the page loads. Then, when a user clicks the ".alert-close" element within one of the popu ...