Jquery Triggers Failing to Work Following Ajax Request

I have worked on 2 PHP pages, called "booking.php" and "fetch_book_time.php".

Within my booking.php (where the jquery trigger is)

<?php
    include ("conn.php");
    include ("functions.php");
?>
$(document).ready(function(){
    $(".form-group").on("change", ".ajax-venue", function(){
        $(".ajax-date").trigger("change"); // if .ajax-venue is changed,
    });                                    // then trigger change on .ajax-date

    $(".form-group").on("change", ".ajax-date", function(){
        // var getData = all my data here

        $.ajax({
            type: "POST",
            url: "fetch_book_time.php",
            data: getData,
            cache: false,
            success: function(getTime){
                $("#ajax-time").html(getTime);
            }
        });
    });
});

The HTML

<div class="form-group">
    Venue: <br>
    <select id='ajax-venue' class="ajax-venue">
        <?php
        $sql = mysqli_query($conn,"SELECT * FROM slot");
        while($row=mysqli_fetch_array($sql)){
            echo "<option value='".$row['slot_venue']."'>".$row['slot_venue']."</option>";
        }
        ?>
    </select>
</div>
<div class="form-group">
    Date: <br>
    <input id="ajax-date" type="date" class="ajax-date"></input>
</div>
<div id="ajax-time" class="form-group">
    Start Time:<br>
    <input id="ajax-sTime" class="ajax-sTime" type="time"></input><br>
    End Time:<br>
    <input id="ajax-eTime" class="ajax-eTime" type="time"></input>
</div>

inside my fetch_book_time.php (ajax page)

<?php
    include ("conn.php");
    include ("functions.php");

    // code and logic here then echo back the codes below

    // input sent back to booking.php into #ajax-time
?>

After spending hours troubleshooting, I discovered that the issue was with the include files in fetch_book_time.php.

If I remove the include files from fetch_book_time.php, the Jquery triggers can function as intended, which is to trigger a change on the date input every time the venue is changed.

Any assistance or suggestions would be greatly appreciated, as I am still learning Jquery and hoping to make progress in this project. Thank you.

Answer №1

since you are injecting new HTML into the page, you need to bind your events after each addition.

success: function(getTime){
                $("#ajax-time").html(getTime);
            }

Alternatively, define your events globally on the document:

Instead of

$(".form-group").on("change", ".ajax-venue", function(){

Use:

$(document).on("change", ".form-group.ajax-venue", function(){

and

$(".form-group").on("change", ".ajax-date", function(){

Use:

$(document).on("change", ".form-group.ajax-date", function(){

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

Modifying the dimensions of a text input box within an HTML string using React

I'm currently utilizing a popup library called sweetalert2-react-content to generate a popup box with a textbox and a text area. Even though the elements are being created successfully, I am struggling to adjust the size of the text area in the popupb ...

Customize WordPress pages by adding a variety of unique logos for a

I am currently customizing a WordPress theme (Increate themes) for a client. They are looking to have a page with different content and a unique logo. My question is, how can I change the logo specifically for this page? This is the section of code in my ...

ajax calls every time you click on tabs

Is there a way to enhance this simple file so that when a tab is clicked, the page content is updated or reloaded? $(function(){ $('#tabsSlide #nav li a').click(function(){ var currentNum = $(this).attr('id').slice(-1 ...

Input for uncomplicated changing identifier

I am looking to create types for dynamic keys that will be of type number. I have two similar types defined as follows: type UseCalculatePayments = () => { totalPayments: number; aggregate: number; condition: boolean; }; type UseCalculateCommissio ...

Sequelize Authentication is unable to finalize

As I delve into the world of node.js and sequelize, I am encountering a persistent error: /home/cbaket/test/test.js:9 .complete(function(err) { ^ TypeError: undefined is not a function at Object.<anonymous> (/home/cbaket/test/test.js:9: ...

Apologies, the system encountered an issue while trying to access the "name" property which was undefined. Fortunately, after refreshing the page, the error was successfully resolved

When the profile route is accessed, the code below is executed: import React, { useState, useEffect } from 'react' import { Row, Col, Form, Button } from 'react-bootstrap' import { useDispatch, useSelector } from 'react-redux' ...

Finding maximum values by key in Mongoose

Welcome to My Schema let schema = new mongoose.Schema({ property: String, numericValue: Number }, { timestamps: true }); In my database, I store various statistics like: let entryA = { property: "visitors", numericValue: 120 } let en ...

populate 3 input fields using the dropdown menu with the help of AJAX

Hello, I am new to PHP and Ajax. I have a select box where users can choose an option, upon which information should automatically fill in four text boxes. Someone recommended using jQuery my initial code. Here is my PHP code: if(isset($_GET['userna ...

Is there a way to make a text area box visible using JavaScript?

Currently, I am developing an automation script in Python using Selenium. My objective is to make a textarea box visible, as I need to insert some arguments into it. Here is the code snippet that I am utilizing: element = driver.find_element_by_id('g ...

Tips for hiding a div element until its visibility is toggled:- Set the display property of

Looking for some guidance on this jQuery code I'm working with to create a toggle menu. The goal is to have the menu hidden when the page loads, and then revealed when a button is clicked. However, currently the menu starts off being visible instead o ...

ezplatform retrieving the URL of an image corresponding to the current language and site being accessed

What is the recommended approach for retrieving the uri of an image in the current translation of the site using twig? We have a object with a translatable image field. Displaying the image using ez_render_field helper functions as expected. However, I n ...

The property fetchPriority is not a valid attribute for HTMLLinkElement

The HTMLLinkElement model has a property mentioned above (as shown in the image), yet the compiler is indicating that the property does not exist. Interestingly, there is one reference visible (the reference I have included in my component). I've cre ...

I'm wondering about retrieving the latest cart session array and showing it in a separate PHP file using AJAX. How can I

AJAX $(document).ready(function(){ //execute removal of product from cart on button click $(".delete-product-cart").click(function(e){ var id = $(this).data('id'); $.ajax({ url: "remove ...

The cursor remains positioned below the video within the designated div

I'm currently facing an issue in a project where the cursor div I created stays underneath the video element. No matter what I do, I can't seem to bring it to the front. Even setting a z-index on the video tag hasn't helped. Can someone plea ...

Troubleshooting Issues with Integrating Bootstrap Carousel

UPDATE: When I attempt to click on either the left or right icons, it unexpectedly scrolls me down to the bottom of the page. It seems to be related to the #truespeed aspect. The carousel functions perfectly when it is on a standalone page but causes issue ...

Guide to adding a new Vue-Grid-Item with a button

Currently, I am working on a software project for the Research department at my university, specifically focusing on an Atomic Layer Deposition system. The main goal of this program is to allow users to create and customize their own 'recipe' for ...

Expressjs route encountering issue with imported database function failing to return a value

Currently, I am in the process of creating a REST API using Expressjs. Initially, all routes were integrated into one main file. However, I have now separated these routes, database connection, and database methods into their individual files. login-db.js ...

Proper method for incorporating a single database pool across an Express application

Disclaimer: This issue pertains to singleton objects in Node.js and not DB pools. While developing an Express.js application with the mysqljs node module for DB connections, I am interested in creating a single pool object that can be reused across differ ...

What is the best way to incorporate (+/-) buttons to increase or decrease numbers in this code snippet?

Hey everyone, I hope you're all doing well! I need some assistance in transforming this quantity picker from a dropdown to have buttons like the one shown here with plus and minus symbols: https://i.stack.imgur.com/O0uUa.png I would like to impleme ...

What exactly is the purpose of utilizing node js and can someone explain to me what constitutes a

After mastering HTML and CSS, I've started diving into JavaScript. However, I'm curious about Node.js. What exactly is it, why do I need it, and can you explain what a framework is in general? Sorry if these questions sound silly! ...