Just a quick question about using AJAX

Before submitting my PHP page, I need to send an email. The mail script is in a file called sendmails.php. Is it possible to use JavaScript to send an AJAX request to send the email before submitting the page? Here is an example:

function submit_page()
{
//trying to run send_mail.php
..............................//ajax codes
............................
xmlhttp.open("GET","send_mail.php",true);
xmlhttp.send();
.................................

if(a)
form.action = 'one.php'
else
form.action = 'two.php'
form.submit()//form submitting using javascript
}

Will this method execute the send_mail.php file on the server?

Appreciate your help.

Answer №1

Absolutely, however, it is not wise to assume that this occurs prior to send() returning. The function send() merely initiates a separate thread that will ultimately establish a connection to send_mail.php and submit the form. Consequently, there is a possibility that the form may be submitted prior to the email being dispatched.

Answer №2

It's highly unlikely - the execution of ajax requests requires a specific timeframe to finalize, and if you leave the page right away, it may disrupt the request.

My advice is to execute the ajax request in synchronous mode. This ensures it will be fully processed before submitting the form.

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

NextJs application displaying empty homepage and preventing redirection

After successfully deploying my nextjs app on netlify, I encountered a strange issue. When I visit the base url of my website, instead of seeing the homepage, all I get is a blank screen. Oddly enough, if I navigate to specific pages on my site, they load ...

Headers cannot be set once they have already been sent in an express js application

After reviewing various responses to this query, I am baffled as to why this error keeps appearing despite having res.send() in multiple paths. In my code (expressjs 4.13), it looks something like this: var user ={ username: "some", password: "a" ...

JavaScript code that functions similarly to VLOOKUP, allowing you to map input values from one field to

As a beginner in HTML and JavaScript, I am trying to create a simple form that automatically populates a specific "Customer Code" when a "Customer Name" is selected from a dropdown list (similar to an Excel VLOOKUP). However, the examples I found on Stack ...

Unique title: "Curved Blocks with Unusual Shapes in SVG Path

Hey there, I'm new to working with SVG and Highcharts. I found a jsfiddle online that I would like to customize for my project, but I've encountered an issue with the rounded corner blocks of lines in the Result panel. Here is the link to the js ...

Exploring the Comparison of Arrays in AngularJS

I am currently working with two arrays: one for Usernames and another for userRoles. The structure of the arrays is as follows: Usernames = [ { "id": 1, "userName": "Jack", "description": "jack is a nice guy", "userRoleIds": [ 1 ...

Connect the dxSelectBox to the button click event

Currently, I am working with the DevExtreme MVVM architecture. In my specific situation, I am trying to bind a dxSelectBox (combo box) upon a button click event. Here is the HTML CODE snippet: <div data-bind="dxButton:{onClick:display,text:'Click ...

Utilizing Data Filters to Embed HTML in Vue.js?

I have been attempting to utilize the Filter feature in Vue.js to insert HTML tags within a String. The documentation indicates that this should be achievable, however, I am not having any success. The objective is for the data to be just a String that is ...

Performing synchronized execution in a node.js application by utilizing the 'readline' package

Struggling with the asynchronous nature of node.js, despite hours spent on callbacks and researching. I have a program that reads lines from a file using the readline module in node, passing data to async functions within the program. The goal is to proces ...

Searching for first and last names in MySQL

So, I'm working with a database table named players that contains data for each player including their forename and surname. I've also implemented a PHP Ajax search feature to allow users to look up players. For instance, if someone types in "Jam ...

Navigate to a new page on button click using Row with Tanstack / React-Table and Typescript (2339)

Encountering a linting error when attempting to navigate to a new route by clicking on a table row. The functionality is working but how can I resolve this issue? It's showing an error message stating "The property "id" for type TData does not exist." ...

Communicate through PHP and JavaScript chat to display HTML content in the chat window

I have been attempting to display HTML output in the chat window but instead, it is showing the HTML code. Here are the two files involved in the chat system: chat.js `function chatHeartbeat(){ var itemsfound = 0; if (windowFocus == false) { var ...

What is the process for obtaining the updated file name of a file that has been saved using ajax AsyncFile

When utilizing the asyncfileupload ajax plugin from the ajax toolkit to save a file, I am running into an issue where the filename needs to be changed to prevent duplicates. To inform the user of the new filename after upload, I have implemented the follo ...

The jQuery $.change function is not functioning properly when used with a cloned select element

In my table, there is a button that allows you to add a new row by cloning the last one. Each row contains a <select> with a different name (0-9), all having the class .variable_priority. When clicking the button, the row clones successfully and the ...

A guide to setting up a queue for ajax requests with jQuery

How can I create an efficient Ajax request queue using jQuery? My goal is to: Allow users to trigger multiple Ajax requests in a web page, which should be queued and processed sequentially. Update the web page based on server responses. Implement e ...

initiating AngularJS ng-model pipeline on blur event

My $parser function restricts the number of characters a user can enter: var maxLength = attrs['limit'] ? parseInt(attrs['limit']) : 11; function fromUser(inputText) { if (inputText) { if (inputText.length > max ...

What is the best way to establish a connection between the same port on Expressjs and Socket.io

I am currently using Express.js and Socket.io to develop a chat application. Initially, I created my project with Express-Generator and began by running the node ./bin/www script. However, I decided to remove the ./bin/www file and instead combined it wit ...

Adjust the color of the entire modal

I'm working with a react native modal and encountering an issue where the backgroundColor I apply is only showing at the top of the modal. How can I ensure that the color fills the entire modal view? Any suggestions on how to fix this problem and mak ...

I am unable to retrieve the local variable beyond the function

I'm having trouble accessing a local variable outside of a function even though I have initialized it before the function is called. <!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jq ...

Tips on Avoiding Initial Click Activation

I've been working on a JavaScript function that dynamically generates an iframe with a button that, when clicked, deletes the iframe itself. Here are the functions I've written: function createIframe (iframeName, width, height) { var ifram ...

Capybara in Rails performing render action post form submission

Within my Rails controller, I have the following code: def update render @bill if @bill.update(bill_params) end In the form, I am using remote true to load the render on the page via AJAX. However, when running Capybara tests, the following code is us ...