I aim to display a success message upon form submission

Can someone assist me with a problem I'm facing? I need to display a success message after successfully submitting a form. I'm utilizing bootstrap and jQuery for this task, along with an email service known as emailJS. This service enables us to send form data directly to our email. Here's the code that I have:

Below is my code snippet:

function submitForm(e) {
  var temp = {
    name: document.querySelector('#name').value,
    email: document.querySelector('#email').value,
    city: document.querySelector('#city').value,
    state: document.querySelector('#state').value
  };
  emailjs.send('service-register', 'register', temp)
    .then($('#messages').removeClass('hide').addClass('alert alert-success alert-dismissible').slideDown().show())
}
<form action="javascript:submitForm()" id="register-form">
  <img src="img/logo.svg">
  <h2 class="title">Your Information</h2>
  <div class="input-div one">
    <div class="i">
      <i class="fas fa-user"></i>
    </div>
    <div class="div">
      <h5>Name</h5>
      <input id="name" type="text" class="input">
    </div>
  </div>
  <div class="input-div pass">
    <div class="i">
      <i class="far fa-envelope"></i>
    </div>
    <div class="div">
      <h5>E-mail</h5>
      <input id="email" type="email" class="input">
    </div>
  </div>
  <div class="input-div pass">
    <div class="i">
      <i class="fas fa-city"></i>
    </div>
    <div class="div">
      <h5>City</h5>
      <input id="city" type="text" class="input">
    </div>
  </div>
  <div class="input-div pass">
    <div class="i">
      <i class="fas fa-map-signs"></i>
    </div>
    <div class="div">
      <h5>State</h5>
      <input id="state" type="text" class="input">
    </div>
  </div>

  <button type="submit" class="btn">Register</button>
  <div id="messages" class="hide">
    <button type="button" id="close" class="close" data-dismiss="alert" aria-hidden="true">×</button> Thank you for getting in touch!
  </div>
</form>

The data is being sent correctly, but the bootstrap alert is not displaying. Any help would be appreciated. Thank you!

Answer №1

According to the documentation, emailjs.send returns a Promise.

To properly handle this promise, you should use an anonymous or arrow function in the .then method. This function will be called when the promise is fulfilled. For example:

 emailjs.send('service-register', 'register', temp)
        .then(function (response) {
            $('#messages')
             .removeClass('hide')
             .addClass('alert alert-success alert-dismissible')
             .slideDown()
             .show()
        });

If the above code still does not work, try calling your jQuery code after the email send call.

 emailjs.send('service-register', 'register', temp);

 $('#messages')
  .removeClass('hide')
  .addClass('alert alert-success alert-dismissible')
  .slideDown()
  .show();
    

If neither of these solutions work, you may need to troubleshoot and ensure that your jQuery code is functioning correctly.

Instead of chaining methods, consider breaking them out into separate lines and stepping through each line with a debugger. Start with simple actions like removeClass and then addClass, gradually building from there.

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

Storing files in DynamoDB using Reactjs is a convenient and efficient way

Is there a way to store resume files in an existing DynamoDB table that currently stores name and email information? I have attempted to do so using React's AWS package with the following code: <input name="my_file" onChange={e => upd ...

Tips for utilizing MUI Typography properties in version 5

I'm clear on what needs to be done: obtain the type definition for Typography.variant. However, I'm a bit uncertain on how to actually get these. interface TextProps { variant?: string component?: string onClick?: (event: React.MouseEvent&l ...

Order of execution and loading of JavaScript files in Webkit threading

I'm currently working on creating an XSS widget and I'm encountering some issues specifically with Webkit browsers when loading external JavaScript files that I'm appending to the DOM. Here's how it's set up: Widget.js appends 3 ...

Is there a way to deactivate other dropdown options?

To simplify the selection process, I would like to disable the options for "Province", "City", and "Barangay". When the user clicks on the "Region" field, the corresponding "Province" options should be enabled. Then, when a specific province is selected, t ...

Error message when trying to get tree from Json using jqTree: "Oops! $(...).tree is not a valid function."

Currently, I am utilizing jqTree to display JSON data in a tree format. However, as I was implementing the demo of jqTree, an error occurred: "Uncaught TypeError: $(...).tree is not a function" ...

Learn the technique to display a table column exclusively in the mobile view using Bootstrap 4

We are presenting a table in our project which contains numerous columns. For the mobile view, we need to add an extra column to the Bootstrap 4 table. While we do have the option to hide the row for all views, it is not the most optimal solution. Is the ...

"Interactive elements like dropdown menus and clickable buttons that transform the content of an HTML

My webpage includes three buttons that, when clicked, reveal hidden div elements containing forms. Here is the HTML code for the buttons: <button id="edituser" type="submit" onclick="toggle_visibility('c');" style="border:0;width:100px;margin ...

Is it possible to create a MongoDB query that can retrieve a specific number of documents from different types within a single collection?

If I have a collection named "pets" with three different types of animals: cat, dog, and bird What if there are 10 cats, 10 dogs, and 10 birds in the collection (30 documents total)? Can I create a query that retrieves 3 cats, 2 dogs, and 1 bird all at o ...

Error in Angular Due to Circular Reference in JSON

I'm currently working on integrating a tree structure in AngularJS. The goal is to construct the tree by dynamically adding nodes based on user input from an Angular Select element. Here is the specific operation I'm trying to accomplish: var a ...

What are the best ways to improve ajax response in jQuery?

My database query is fetching around 5000 rows of data along with data from 5 relational tables, leading to performance issues. The network tab shows the size of the request resource as 9.1 mb. After that, I am dynamically adding this data to a table using ...

When using selenium with python, the function excecute_script('return variable') may not technically return variables, even though the variable does exist

My attempt to retrieve a variable from javascript code using selenium is encountering difficulties. Despite the presence of the variable (confirmed by inspecting the source code before executing the script), the command driver.execute_script('return v ...

Having difficulty retrieving the value of the final <th> element within a table or list

I am encountering an issue where I cannot retrieve the value of the last item and update the next item accordingly. I have attempted using :last-child, but it does not provide the desired values. if ($("tbody").children().length == 0) { var ...

Display a different image while another image is in the process of loading

I am currently facing an issue with a div that has a background image set up as follows: <div class="carousel-image" style="background: url(img/background.gif) no-repeat center center;"> </div> Upon loading my webpage, there is a brief ...

Modify mouse pointer when an object is clicked using JavaScript

Greetings, I am in the process of designing a website for a client. I have encountered a challenge in changing the cursor icon when a user performs a mousedown on an object. There is an image on the webpage When the user clicks on the image, the cursor s ...

Tips for positioning a highcharts pie chart and legend in the middle of a page

I'm struggling to center my highchart data in a node/react single page application. Currently, it appears off-center like this: https://i.stack.imgur.com/ccR6N.png The chart is floating to the left and I would like to have everything centered within ...

Connecting a href link to a TAB

Check out this useful CODE example I am using. I have a webpage with links that have href attributes. Now, I want to link these pages to another page and have them automatically open a specific tab when clicked. Is this possible? Here are the links on th ...

Is using float:right making the jquery slide toggle dropdown div (triggered by hover) appear glitchy?

Utilizing jQuery's slidetoggle and hover functions, I have successfully implemented a dropdown feature in my div. Within this div, there is various information displayed including the date, a note counter, and three buttons. The first two are Tumblr ...

What is the process for retrieving the members of an ActiveDirectory 2 group through code?

I have been using ActiveDirectory2 to query LDAP in order to retrieve the users of a specific group, but unfortunately, I have not been successful so far. Here is an example of how I am using it: ad.authenticate(config.USERNAME, config.PASSWORD, function ...

Create a Vue JS component that enables the rendering of checkbox inputs and sends the selected values back to the

I am working on a Vue JS project where I am creating a custom component to display multiple checkboxes on the page. The challenge I am facing is sending back the value to the component in order to attach a v-model to it. Currently, all my checkboxes allow ...

Refresh the Content of a Page Using AJAX by Forcing a Full Reload

I have a webpage that automatically updates a section using jQuery AJAX every 10 seconds. Whenever I modify the CSS or JavaScript of that page, I would like to include a script in the content fetched via AJAX to trigger a full page reload. The page is ac ...