Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8167903
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T20:21:00+00:00 2026-06-06T20:21:00+00:00

Hi i am new and i love this site I am learning javascript and

  • 0

Hi i am new and i love this site

I am learning javascript and so far i have done a lot of tutorials but i can’t make ajax work.

I saw this on W3school

   var xmlhttp;
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("myDiv").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","ajax_info.txt",true);
xmlhttp.send();

Now i changed one line to

xmlhttp.open("GET","ajax_info.php?day="+document.getElementById("day").value,true);

It should read info based on a day instead of all of the days.

Sometimes the page is empty, nothing happens, sometimes it works and everything is there.

To reproduce you can just hit F5 like 3 times in a row, it will fail at least once.

Please help me save the last bits of hair i still have !

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T20:21:02+00:00Added an answer on June 6, 2026 at 8:21 pm

    The problem is caused by the script loading too fast that the “day” element is not there yet.

    To fix you can simply add an event handler to wait for the document to load.

    You can do this like that : document.addEventListener("load", functionName, false); and then create a function that will hold the code you pasted here.

    The function will be called at the appropriate time and it should work.

    Other than that, i would recommend using libraries like jQuery or MooTools as it makes it easier for your code to run across many browsers. In your code, you are checking to see which browser the user has to create the request..

    With jquery you can do all your code with a single line of code like so:

    $(document).ready(function(){   // Wait for the document to load
        $.get("http://www.website.com?day="+$("#day").val());   // $.get will create an ajax, $("#day") is equivalent to getting an element by Id
    });
    

    You can learn more at http://www.jquery.com

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm pretty new to scala and I really love it so far but I'm
New to this community... I have to say I love this resource though, it's
I love the new DATE datatype in SQL Server 2008, but when I compare
Scenario: Quite new to DI and Ninject but would love to master it so
I used to love using IntelliJ but my new company doesn't spend money on
I'm fairly new (ok, REALLy new) to generics but I love the idea of
I'm pretty new to wordpress, html, css, and javascript and would love your help
I love this plugin: http://jquery.malsup.com/block/#demos The iPhoto (ish) one works beautifully on my site
I have to compile a game like this love C:\testgame in the cmd. So
I'm working on a new app and would love to have the following feature.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.