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

  • SEARCH
  • Home
  • 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 8837109
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T09:38:38+00:00 2026-06-14T09:38:38+00:00

I am trying to build a page that will display the next item in

  • 0

I am trying to build a page that will display the next item in a PHP array when the user clicks on a button. I’m a complete newbie on Ajax, and I am using the “Head First Ajax” book as a guide. My code (below) doesn’t work and I don’t understand why. Quite frankly, I am not even sure how to debug Ajax…

The code is in a file called Index.php. I am using WampServer.

The page loads properly. Here is the code:

<html><head><script type="text/javascript">
  var id=null;
  function getRequest(){
    try {
      request = new XMLHttpRequest();
    }catch (MS){
      try {
        request = new ActiveXObject("Msxml2.XMLHTTP");
      }catch (MoreMS){
        try {
          request = new ActiveXObject("Microsoft.XMLHTTP");
        }catch (failure){
          request = null;
        }
      }
    }
    return request;
  }

  function showNext(itemId){
    request=getRequest();
    if(request==null){
      alert("Request object is null");
      return;
    }
    var url="index.php?itemId="+escape(itemId);
    request.open("GET",url,true);
    request.onreadystatechange=function(){
      if(request.readyState==4){
        alert("Request.readyState is 4");
        if(request.status==200){
          alert("Request.status is 200");
          document.getElementById('article').value=request.responseText;
        }else{
          alert("Request.status not 200");
          return;
        }
      }else{
        alert("Request.readystate not 4");
        return;
      }
    }
    request.send(null);
  }

  function getItemId(){
    if(id==null){
      id=0;
    }else{
      id=id+1;
    }
    return id;
  }

  function doIt(){
    itemId=getItemId();
    showNext(itemId);
  }
</script></head>
<body><p>
  <textarea id='article'>
  </textarea>
</p>  
<p>
  <button type="button" id='showNext1' onclick="doIt()" >Show next</button>
</p></body></html>

<?php
  $array=array(10,21,32,43,54,65,76);
  if(!isset($_GET['itemId'])){
  }else{
    echo $array[$_GET['itemId']];
  }
  var_dump("GET :",$_GET);
?>

When I click the button, here is what happens:

I get twice the Request.readystate not 4 alert, then once Request.readyState is 4, then once Request.status is 200, then the textarea gets populated, and then again once Request.readystate not 4. After all that the textarea contains my entire code followed by this:

10<pre class='xdebug-var-dump' dir='ltr'><small>string</small> <font color='#cc0000'>'GET :'</font> <i>(length=5)</i>
</pre><pre class='xdebug-var-dump' dir='ltr'>
<b>array</b>
  'itemId' <font color='#888a85'>=&gt;</font> <small>string</small> <font color='#cc0000'>'0'</font> <i>(length=1)</i>
</pre>

Then if I click again I get the same alerts and output except that at the end I get 21<pre class='xdebug-var-dump'..... Next click produce the same except with 32<pre class.... at the end, etc.

  • 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-14T09:38:39+00:00Added an answer on June 14, 2026 at 9:38 am

    Try defining the function for onreadystatechange event inline. Also, the textarea’s value is accessed using value, not innerHtml. The modified showNext() would be:

    function showNext(itemId){
        request=getRequest();
        if(request==null){
            return;
        }
        var url="index.php?itemId="+escape(itemId);
        request.open("GET",url,true);
        request.onreadystatechange= function(){
            if(request.readyState==4){
                if(request.status==200){
                document.getElementById('article').value=request.responseText;
                }
            }   
        }
        request.send(null);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create a page in Wordpress that will display the user's Facebook
Im trying to build a page that will allow a user to select a
I am trying to build a page that will allow me to take an
I am trying to build a simple page that will show book information, I
I'm trying to build an admin settings page that will allow users to upload
I'm trying to build an asp.net page using c# that will query a column
I'm trying to build an app that will display stats for a certain game
I'm trying to build a mobile content application that will be used to display
I'm trying to build a process that will repopulate an item that was inserted
I'm trying to build a Web page that has a rich text editor. Is

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.