After ajax, trigger change event

Can anyone assist me in combining multiple functions within the same form using AJAX? The purpose of the form is to prenote a new "meeting" and it consists of an input for the date and a select dropdown for choosing the operator.

FORM CODE:

<div id="info"></div>
<form>
<div id="input-form">
  <input type="date" name="data" id="dataApp" onChange="checkDate()" required>
</div>
<div id="divSquadre">
  <select name="squadra" onChange="orariApp()" id="squadra" required>
  <option value="0">Operator 1</option>
  <option value="1">Operator 2</option>
  <option value="2">Operator 3</option>
  </select>
</div>
</form>

The first function, checkDate, validates the input date in the database and updates the select dropdown with only the available operators. The second function, orariApp, currently displays an alert when called, serving as a debugging tool.

JS CODE:

function checkDate() {
      var data=document.getElementById("dataApp").value;

      var xmlhttp = new XMLHttpRequest();
      xmlhttp.onreadystatechange = function() {
        if (this.readyState == 4 && this.status == 200) {
          document.getElementById("divSquadre").innerHTML = this.responseText;
        }
      };
      xmlhttp.open("GET", "./ajax/checkSquadreInDataApp.php?data=" + data , true);
      xmlhttp.send();
    }

function orariApp() {

  //if run function checkDate() this function doesn't work
  alert("i'm working :)");

}

PHP checkSquadreInDataApp.php:

<?php
$data=$_REQUEST["data"];
$query = $mysqli->query('SELECT * FROM operators where data like "'.$data.'" and active is not null'); //example query
$squadra=$query->fetch_all(MYSQLI_BOTH);
echo '<select name="squadra" onChange="orariApp()" id="squadra" required>';
foreach($squadra as $el){
  echo '<option value="'.$el['id'].'">'.$el['id'].'</option>';
}
echo '</select>';

Prior to changing the date (initiating the checkDate function), the orariApp function works fine. However, once the checkDate function modifies the div "divSquadra," the orariApp function stops functioning. Any assistance would be greatly appreciated!

Apologies for any language errors in English :)

Answer №1

Greetings! I am attempting to retrieve the value of the selected option using the "orariApp" function. The function works fine if I do not change the date manually (without AJAX). However, when the "checkDate" function sends an HTTP Request and alters the select option, the "orariApp" function ceases to work.

Hey Nik, I get it. JQuery-generated contents added to the DOM can sometimes have issues. Here's a solution you can try in your scenario; add this to your function:

    function orariApp() {

  //if checkDate() is executed, this function won't work
  alert("I'm working :)");

}

in the responsecheckSquadreInDataApp.php file. It might resolve the issue. Keep me posted on any progress.

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

I'm having trouble understanding why my data does not appear even though there is space allocated for it automatically

I'm facing an issue with my code that is supposed to populate a table with dynamic content using react and ant.design. Everything seems to be working fine, but the actual content is not displaying - only the space for it is created. What's happe ...

Combining React, Express, and Nodemailer poses challenges in rendering components and sending emails simultaneously

Looking to utilize client-side routing for my React app and also incorporate Nodemailer for sending emails. However, since Nodemailer cannot be used on the client-side, I need to implement it on the Express server. Here is how the server code looks like: ...

Transferring Live Information Between Two Controllers

How can I transfer the 'header' and 'content' from a controller's $scope element (specifically the 'header' and 'content') to another page that is redirected to upon clicking a button? The current setup involve ...

Create a function in JavaScript that can generate a random number a specified number of times based on user input

I have been struggling to develop a JavaScript program that can generate a random number multiple times based on user input. Despite numerous attempts, I haven't been able to get it to work. Below is the code snippet I've been working with: f ...

Displaying feedback messages and redirecting in Flask may result in the response being visible in the developer tools, but

Just starting out with Flask and encountering an issue. I am trying to determine if a response is empty, and if it is, display a message. The problem lies in the fact that even though the redirect works and the subsequent GET request returns the correct HT ...

After the click event, the variable in the Angular .ts file does not get refreshed

Great! I have a service in my .ts component that loops through an array of court names. Every time I click on a next or back arrow event, a counter is incremented starting at 0, where index 0 corresponds to field 1 and so on. The issue I'm facing is ...

Unleashing the y-axis and enabling infinite rotation in Three.js

In my three.js project, I am utilizing orbital controls. By default, the controls only allow rotation of 180 degrees along the y-axis. However, I would like to unlock this restriction so that I can rotate my camera infinitely along the y-axis. As someone ...

Ways to eliminate dates from the text of listed items

Before finalizing their registration, users on our site are shown a review page. This panel displays all the items they have selected, creating a unique and variable list for each individual. However, each item in the list starts with a distracting date/ti ...

What causes the Invariant Violation: Invariant Violation: Maximum update depth exceeded error to occur when using setState()?

Looking for some help with this component: // here are the necessary imports export default class TabViewExample extends Component { state = { index: 0, routes: [ { key: 'first', title: 'Drop-Off', selected: true }, ...

What methods can I use to minimize the frequency of the blue box appearing around text?

I successfully developed code that enables user interaction with text, triggering the opening of a modal box when clicked. In this modal box, the text turns red upon clicking, indicating activation of a checkbox. However, I notice that every time I intera ...

What sets apart importing components in computed from importing components in dynamic import in Vue.js?

Imagine there are 4 components available on a page or within a component, and the user can utilize them based on their selection by toggling between Input, Image, Video, or Vudio components. There are two ways to lazily load these components: 1) <temp ...

Dealing with JSON data retrieved from a Django QuerySet using AJAX

I am utilizing a Django QuerySet as a JSON response within a Django view. def loadSelectedTemplate(request): if request.is_ajax and request.method == "GET": templateID = request.GET.get("templateID", None) ...

steps for setting up babel-cli and babel-preset-react

I attempted various methods of installing babel-cli babel-preset-react Here's what I tried: npm install --save-dev babel-cli babel-preset-react However, when I run babel -h An error message appears saying The program 'babel' can be found ...

Unusual output from the new Date() function: it displays the upcoming month

Your assistance and explanation are greatly appreciated. I have created a method that is supposed to return all the days of a given month by using two parameters- the year and the month: private _getDaysOfMonth(year: number, month: number): Array<Date& ...

How can I use JavaScript to convert a JSON object into an array for iteration with ng-repeat?

Upon retrieving my JSON object from Firebase, I am faced with the challenge of converting a list into an array for binding in HTML using ng-repeat. The structure of my JSON object is as follows: { "cats1": { "Name": "cricket", "imgUrl": "some ...

Distributing Back-end Information to a JavaScript File

Recently, I developed a blog site from scratch that includes features such as posts, users, and comments. To build this site, I utilized MongoDB, NodeJS, and Express while implementing an EJS view. However, I encountered an issue when attempting to create ...

Leveraging ACE in conjunction with WT

UPDATE 3 Here is the finalized working code below. Remember to use ace.js from the src folder, as it will not work from the libs directory. You need the pre-packaged version from their site. WText *editor = new WText(root()); editor->setText("function( ...

"Implementing a click event on a dynamically generated element is triggering the event for all of its parent elements

I have a task to generate a dynamic table with data retrieved from the server. Each row in the table contains a tag that I am trying to link to a click event. The code snippet below demonstrates how the dynamic table is created: function ProcessResponse ...

Problems with select tag change events

I encountered an issue with select tag onChange events. When I select a value from the select tag, it should display in a textbox that is declared in the script. It works perfectly when I remove the class "input" from the select tag, but I prefer not to re ...

VUE JS - My methods are triggering without any explicit invocation from my side

I've encountered a frustrating problem with Vue JS >.<. My methods are being triggered unexpectedly. I have a button that is supposed to execute a specific method, but this method gets executed along with other methods, causing annoyance... Her ...