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 7571031
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T15:34:12+00:00 2026-05-30T15:34:12+00:00

I am trying to use AJAX for the first time, and I am having

  • 0

I am trying to use AJAX for the first time, and I am having a difficult time getting the hang of it. Here is the request code:

function requestData(j) {
    var xmlhttp;
        if (window.XMLHttpRequest) {
            xmlhttp=new XMLHttpRequest();
        }
        xmlhttp.onreadystatechange = function() {
                if (xmlhttp.readyState==4 && xmlhttp.status==200) {
                    xml = xmlhttp.responseXML;
                    post = "";
                    title = "";
                    postdata = xml.getElementsByTagName("post");
                    titledata = xml.getElementsByTagName("title");
                    datedata = xml.getElementsByTagName("date");
                    timedata = xml.getElementsByTagName("time");
                    document.getElementById("post").value = postdata[0].childNodes[0].nodeValue;
                    document.getElementById("heading").value = titledata[0].childNodes[0].nodeValue;
                    document.getElementById("date").value = datedata[0].childNodes[0].nodeValue;
                    document.getElementById("time").value = timedata[0].childNodes[0].nodeValue;
                    document.getElementById("id").value = j;
                    document.getElementById("update").value = "true";
                }
            };
        xmlhttp.open("POST","../script/getnewsdata.php",true);
        xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
        xmlhttp.send("ID=" + j);
        return false;
    }

Firebug is telling me that “xml is null” on the line postdata = xml.getElementsByTagName(“post”); Which means that xmlhttp.responseXML is null.

This is the server side script:

<?php
$db = mysql_connect("wadafw","awfawf","awfsgv");
if(!$db)
{
  die("Could not connect: " . mysql_error());
}
mysql_select_db("afggbare", $db);
$updata = mysql_query('SELECT * FROM News WHERE NewsID='.$_POST['ID']);
$blog = mysql_fetch_array($updata);
$post = $blog['Content'];
$regex = Array('/<br />/', '/<(\/?)(b|i|u)>/', '/<a href="(http://[www.]?\w+)">(\w+)<\/a>/', '/<div class="media"><iframe title="YouTube video player" width="425" height="265" src="http://www.youtube.com/embed/(\w+)hd=1" frameborder="0" allowfullscreen></iframe></div>/', '/<div class="media"><img width="425" src="(http://[www.]?[\w+])" /></div>/');
$regReplace = Array('\r\n', '[$1$2]', '[link=$1]$2[/link]', '[youtube]http://www.youtube.com/watch?v=$1[/youtube]',
                '[img]$1[/img]');
$post = preg_replace($regex, $regReplace, $post);

echo    '<newsItem>
        <title>'.$blog['Heading'].'</title>
        <post>'.$post.'</post>
        <date>'.$blog['time'].'</date>
        <time>'.$blog['time'].'</time>
    </newsItem>';
?>

The regex is probably bad… but that’s not important right now.

Changed to this… and now getting “function not defined” errors:

function requestData(j) {
        $.ajax("../script/getnewsdata.php", {
            data: {ID: j},
            type: "POST",
            dataType: "xml",
            success: function(data, status, jqXHR){
                var xml = jqXHR.responseXML;
                postdata = xml.getElementsByTagName("post");
                titledata = xml.getElementsByTagName("title");
                datedata = xml.getElementsByTagName("date");
                timedata = xml.getElementsByTagName("time");
                document.getElementById("post").value = postdata[0].childNodes[0].nodeValue;
                document.getElementById("heading").value = titledata[0].childNodes[0].nodeValue;
                document.getElementById("date").value = datedata[0].childNodes[0].nodeValue;
                document.getElementById("time").value = timedata[0].childNodes[0].nodeValue;
                document.getElementById("id").value = j;
                document.getElementById("update").value = "true";
            }


        }
    }

Okay, found that problem. It was just a typo… hadn’t closed the $.ajax() parameter brackets. Now I am not getting errors. But it just doesn’t do anything…

  • 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-05-30T15:34:15+00:00Added an answer on May 30, 2026 at 3:34 pm

    Instead of trying to get AJAX running from scratch, I’d recommend using an existing library. jQuery makes this task extremely easy.

    http://api.jquery.com/jQuery.ajax/

    function requestData(j) {
        $.ajax('../script/getnewsdata.php', {
            data: {ID: j},
            type: 'POST',
            dataType: 'xml',
            success: function(data, status, jqXHR){
                // consume data here
            }
        });
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using HMVC codeigniter. I am trying to use jquery ajax first time.
I'm having all kinds of grief trying to re-use a jQuery-UI dialog. First time
What I'm trying to do is to use ajax to get time from another
I'm having a hard time trying to make jquery.form with a cross-domain request. I'm
I'm trying to use a4j:poll component for ajax reloading a datatable after some time
Good day! I am trying to use jquery for the first time. And i
I using a Ajax-Enabled Wcf service for the first time trying to test a
I am trying to use AJAX for a web app game that im making.
I am currently trying to use AJAX in my application via jRails. I am
I am trying to use .ajax() to post a People object to a MVC2

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.