JQuery requests functioning flawlessly on one system while encountering issues on other systems

I've encountered an issue with the code on my admin page. It used to work perfectly fine on my system, but now it seems to have stopped functioning. My client urgently needs to update this page, however, when I attempt to run it, the JQuery requests are not being performed. What the code does is, upon focus or value change of the first dropdown, the other category and subcategory dropdowns get updated via JQuery requests to another PHP file that returns the category values. Additionally, I've tried running the page in different browsers without any success.

There's also a problem posting the code through a code snippet, so here's the code:

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Cracktitude-Admin</title>
<script src="scripts/jquery.min.js"></script>
    <script>
    $(document).ready(function(){
        $("form").on('submit',function(event){
        event.preventDefault();
            data = $(this).serialize();

            $.ajax({
            type: "GET",
            url: "admin-adddata.php",
            data: data
            }).done(function( msg ) {
            alert( "Data Saved: " + msg );
            location.reload(true);
            });
        });
    });
    </script>

    <script> 
        function categorylist(str) 
        { 
        if (str=="") 
          { 
          document.getElementById("category").innerHTML=""; 
          return; 
          }  
        if (window.XMLHttpRequest) 
          {// code for IE7+, Firefox, Chrome, Opera, Safari 
          xmlhttp=new XMLHttpRequest(); 
          } 
        else 
          {// code for IE6, IE5 
          xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
          } 
        xmlhttp.onreadystatechange=function() 
          { 
          if (xmlhttp.readyState==4 && xmlhttp.status==200) 
            { 
            document.getElementById("category").innerHTML=xmlhttp.responseText; 
            } 
          } 
        xmlhttp.open("GET","admin-getdata.php?choice="+str,true); 
        xmlhttp.send(); 
        } 
    </script>

    <script>

        function subcategorylist(str1) 
        { 
        if (str1=="") 
          { 
          document.getElementById("subcategory").innerHTML=""; 
          return; 
          }  
        if (window.XMLHttpRequest) 
          {// code for IE7+, Firefox, Chrome, Opera, Safari 
          xmlhttp=new XMLHttpRequest(); 
          } 
        else 
          {// code for IE6, IE5 
          xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
          } 
        xmlhttp.onreadystatechange=function() 
          { 
          if (xmlhttp.readyState==4 && xmlhttp.status==200) 
            { 
            document.getElementById("subcategory").innerHTML=xmlhttp.responseText; 
            } 
          } 
        xmlhttp.open("GET","admin-getdata1.php?choice="+str1,true); 
        xmlhttp.send(); 
        } 
    </script> 

<style type="text/css">

body {
    font: 100%/1.4 Verdana, Arial, Helvetica, sans-serif;
    background-color: #42413C;
    margin: 0;
    padding: 0;
    color: #333;
}

ul, ol, dl { 
    padding: 0;
    margin: 0;
}
h1, h2, h3, h4, h5, h6, p {
    margin-top: 0;
    padding-right: 15px;
    padding-left: 15px;}
a img { 
    border: none;
}

a:link {
    color: #42413C;
    text-decoration: underline;
}
a:visited {
    color: #6E6C64;
    text-decoration: underline;
}
a:hover, a:active, a:focus { 
    text-decoration: none;
}

.container {
    width: 960px;
    background-color: #FFF;
    margin: 0 auto; 
}


.content {

    padding: 10px 0;
}

.fltrt {
    float: right;
    margin-left: 8px;
}
.fltlft { 
    float: left;
    margin-right: 8px;
}
.clearfloat {
    clear:both;
    height:0;
    font-size: 1px;
    line-height: 0px;
}

form{
    font-size:14px;
    font-family:Verdana, Geneva, sans-serif;
    color:#333; 
}

form p{
    vertical-align:top;
}

</style>

</head>

<body>

<div class="container">
  <div class="content">
    <h1>Admin Panel</h1>

    <form>
      <p>Section:
        <select name="section" id="section" tabindex="1" onchange="categorylist(this.value)" onfocus="categorylist(this.value)" autofocus="autofocus">
          <option value="Aptitude">Aptitude</option>
          <option value="1">Engineering</option>
          <option value="2">GK</option>
          <option value="3">Interview</option>
          <option value="4">Puzzle &amp; Mind Games</option>
        </select>
      </p>
      <p>Question: 
        <textarea name="question" id="question" cols="45" rows="5" tabindex="2" required="required"></textarea>
      </p>
      <p>Option A: 
        <input type="text" name="optiona" id="optiona" required="required"/>
      </p>
      <p>Option B: 
        <input type="text" name="optionb" id="optionb" required="required"/>
      </p>
      <p>Option C: 
  <input type="text" name="optionc" id="optionc" required="required"/>
      </p>
      <p>Option D: 
        <input type="text" name="optiond" id="optiond" required="required" />
      </p>
      <p>Correct Answer: 
        <label>
          <input type="radio" name="answer" value="A" id="answer_0" />
          Option A |</label>
        <label>
          <input type="radio" name="answer" value="B" id="answer_1" />
        Option B |</label>
        <label>
          <input type="radio" name="answer" value="C" id="answer_2" />
        Option C |</label>
        <label>
          <input type="radio" name="answer" value="D" id="answer_3" />
        Option D</label>
        <br />
      </p>
      <p>Explanation: 
        <textarea name="explanation" id="explanation" cols="45" rows="5"  required="required"></textarea>
      </p>
      <p>Category: 
        <span name="category" id="category">
            <select name="cat">
                <option></option>
            </select>
        </span>
      </p>
      <p>Sub-category: 
        <span name="subcategory" id="subcategory">
            <select name="subcat">
                <option></option>
            </select>
        </span>
      </p>
      <p>Type: 
        <select name="type" id="type">
          <option value="1">I</option>
          <option value="2">II</option>
          <option value="3">III</option>
          <option value="4">IV</option>
        </select>
      </p>
      <p>
        <input type="submit" name="add" id="add" value="Add" /> 
        <input type="reset" name="reset" id="reset" value="Clear" />
      </p>
    </form>
    </div>
  <!-- end .container -->
  </div>
</body>
</html>

Answer №1

Continuing on from previous discussions, here is a helpful resource: http://jsfiddle.net/TrueBlueAussie/4s4d1qgu/1/

Instead of utilizing the onchange= and onfocus= attribute handlers in the HTML code, it is recommended to use jQuery for connecting events to your controls. For example:

$('#section').on('focus change', function () {
    var str = $(this).val();
    if (str == "") {
        document.getElementById("category").innerHTML = "";
        return;
    }
    if (window.XMLHttpRequest) { 
        xmlhttp = new XMLHttpRequest();
    } else { 
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange = function () {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            document.getElementById("category").innerHTML = xmlhttp.responseText;
        }
    }
    xmlhttp.open("GET", "admin-getdata.php?choice=" + str, true);
    xmlhttp.send();
});

If you check the console (e.g., in Chrome), you will observe that the php script is indeed being invoked from the current directory. Therefore, ensure that your page resides in the same folder as the admin-adddata.php and admin-getdata.php files. Consider using tools like Fiddler2 or the Chrome F12 debug Network panel to monitor the requests and responses exchanged with the server. It could be a simple issue such as a 404 error (not found).

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

What is the process for configuring a specific timezone in the datetimepicker?

I recently started using the datetimepicker jQuery plugin available at but I am struggling to understand how to incorporate timezone settings. I have reviewed the documentation provided, however, I could not locate any specific instructions regarding tim ...

Transferring the state from a parent component to a child function

I'm having trouble passing a state from a component to a function. I was able to pass the state from Home to ListHome, but I'm struggling to continue passing it to a function within ListHome (Slider) because it's a function. Even after revi ...

Positioning the <div> to the left of a sidebar

I regret to inform you that I am unable to provide an answer at this time. Below is the code snippet provided: #wrapper { width: 844px; display: block; margin-left: auto; margin-right: auto; text-align: left; } #posts { width: 844px; float: left; } .en ...

JavaScript plugin designed for effortless conversion of JSON data to structured HTML code

I want to add this JSON data to an HTML element. [ { "website":"google", "link":"http://google.com" }, { "website":"facebook", "link":"http://fb.com" } ] Is there an easy way to convert this using a plugin ...

Using 'if' conditions in Reactjs: A step-by-step guide

Working with Reactjs in the nextjs framework, I have received user data that includes the "category name (cat_name)" selected by the user. Now, I need to display that category in a dropdown menu. How can I achieve this? The current code snippet showcases ...

conflicting character encoding systems

Request normally comes in this format: Parameters: {"utf8"=>"✓", "status_id"=>"12686"} However, from one particular client, the request looks like this: Parameters: {"utf8"=>"\xE2??", "status_id"=>"12686"} As a result, I am encounter ...

Attempting to employ jQuery to generate a text input that allows for inputting multiple incorrect answers

I am putting together a webpage for a friend who has allergies. The idea is that users can input a food item, and the page will indicate whether or not my friend is allergic to it. I have compiled an array of his food allergies, and the goal is for the pag ...

jQuery .on event not triggering Bootstrap 5.3 events

This situation is quite peculiar. I have never encountered something like this before... Here is the dropdown code snippet... $('#mydropdown').on('show.bs.dropdown', function() { console.log('triggered 1'); }); $(docu ...

How can I transfer an array of objects obtained from an ajax call to an observable array?

One of my functions involves making an ajax call and receiving data in the callback. It looks something like this: function fetchData(callback) { //perform ajax if(callback) { callback(data.data); } } If I use the function like this: fetc ...

The performance of aframe is being affected by the use of bounding boxes for collision

Hey there! I recently created a component to handle collision detection for primitive and non-primitive shapes. While using the bounding box collision feature provided in three.js, everything was working smoothly. However, when applying it to custom object ...

What is the best way to display text outside of the current div container?

When I include the "Y" in the code snippet below, $title1 and $title2 are positioned outside of the div and centered against the width of the page. However, without the "Y", the text shifts up and aligns with the address class instead. It's puzzling w ...

JSON - The challenge of incorporating single quotes within double quotes

In my current scenario, I am using the following code to populate form fields. The code is designed to handle a JSON dataset that has been encoded with PHP's json_encode function. Everything works smoothly when dealing with either single or double qu ...

Using Selenium and Python to scrape text from a continuously refreshing webpage after each scroll

Currently, I am utilizing Selenium/python to automatically scroll down a social media platform and extract posts. At the moment, I am gathering all the text in one go after scrolling a set number of times (see code below), but my objective is to only gathe ...

Fade in background color with jquery when scrolling

Is there a way to make the header background fade in after scrolling a certain number of pixels? The code I have sort of works, but not quite right. Any suggestions? Thank you! $(function () { $(window).scroll(function () { $(document).scrol ...

Although sanitize and raw display HTML tags, Slim remains unaffected

I have a dataset with HTML content that I want to display without any tags. Currently, I am working with Ruby on Rails (RoR) and Slim templates. I've experimented with sanitize, raw, and html_safe, but none of them fully removed all the HTML tags. ...

Continuously transmitting PNG files to a server. I intend to instruct the receiving browser to automatically refresh at set intervals

I have a continuous task where my code is uploading a png to an http server every few seconds. I am looking for a way to implement some kind of marker that will trigger the browser to automatically refresh every 500ms, either through browser-side javascrip ...

Exploring the JSON structure

Struggling to come up with a solution as I am limited by certain restrictions. I need to create a mobile navigation system using a JSON object provided by the proprietary server. The only allowed framework is jQuery 1.12.4, no other frameworks or updated v ...

Arranging Bootstrap 4 columns in a vertical stack

My HTML code is as follows: <html> <head> <title>Title</title> <meta charset="utf-8"> <meta name="description" content="description"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <li ...

Utilizing JavaScript to dynamically update the user interface after submitting a form using jQuery

I am working on implementing an Input element that triggers a form submission: <input type="submit" value="Download" id="downloadButton" class="btn-download" /> The specific requirement is for the button to first execute a javascript function and t ...

execute a postback to refresh UpdatePanels

My webpage has a jQuery modal that pops up when a button is clicked. After performing some logic in the WebMethod called by this button, I need to refresh multiple update panels on the page to show the changes made. Although I am not very familiar with Up ...