0

I created the xml data from the db, and try to give the output in php page.

function act_list(pagenum) {
    REQ = newXMLHttpRequest();
    var handlerFunction = processReqList;
    REQ.onreadystatechange = handlerFunction;
    if( pagenum == 0 ){
        pagenum = THISPAGE;
    }else{
        THISPAGE = pagenum;
    }
    REQ.open("POST", "act/act_list.php", true);
    REQ.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    REQ.send("page="+pagenum);
}

First, when I requested a function in JavaScript to create an xml file in php

<?php
    require_once "../include/config.php";
    require_once "../include/DBAccess.php";

    $rs = DBSelect("SELECT count(*) FROM {$tableName}");
    $row = mysql_fetch_array($rs);
    $totalCnt = $row[0];

    $thisPage = @(int)$_POST["page"];

    if($thisPage <= 1){
        $startNum = 0;
    }else{
        $startNum = ($thisPage-1) * $listUnit;
    }

    $strSQL = "";
    $strSQL = " SELECT seq, id, title, content, viewcount, write_time ";
    $strSQL .= "  FROM {$tableName} ";
    $strSQL .= " ORDER BY seq DESC ";
    $strSQL .= " LIMIT {$startNum},{$listUnit} ";

    $rs = DBSelect($strSQL);

    $out = "";
    $out = "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"no\"?>\n";
    $out .= "<lists totalcnt='{$totalCnt}'>\n";

    while ( $row = mysql_fetch_array($rs) ) {
        $out .= "  <item seq=\"".$row[0]."\">\n";
        $out .= "    <uname>".$row[1]."</uname>\n";
        $out .= "    <btitle>".$row[2]."</btitle>\n";
        $out .= "    <bcommentcnt>".$row[3]."</bcommentcnt>\n";
        $out .= "    <bviewcnt>".$row[4]."</bviewcnt>\n";
        $out .= "    <bcreatetime>".$row[5]."</bcreatetime>\n";
        $out .= "  </item>\n";
    }

    $out .= "</lists>\n";
    header( "Content-type: application/xml; charset=utf-8" );
    echo $out;
?>

I try to output the generated xml file in php to html table list. However, page not found error occurs only firstChild.nodeValue.

function processReqList() {
    // only if req shows "loaded"
    if (REQ.readyState == 4) {
        // only if "OK"
        if (REQ.status == 200) {
            printList();
        } else {
            alert("There was a problem retrieving the XML data:\n" +
            REQ.statusText + REQ.status);
        }
    }//if
}
function printList() {
    var lists = REQ.responseXML.getElementsByTagName("lists")[0];
    var output = document.getElementById("out");
    var outhtml = "";
    output.innerHTML = "";

    var items = lists.getElementsByTagName("item");

    outhtml += "<TABLE border='0' cellpadding='3' cellspacing='0' align='center' class='list_table'>";

    if(items.length > 0){
        outhtml += "<TR>";
        outhtml += "  <TD class='list_th' width='50' align='center'>NUM</TD>";
        outhtml += "  <TD class='list_th' align='center'>TITLE</TD>";
        outhtml += "  <TD class='list_th' width='70' align='center'>Author</TD>";
        outhtml += "  <TD class='list_th' width='70' align='center'>DATE</TD>";
        outhtml += "  <TD class='list_th' width='40' align='center'>COUNT</TD>";
        outhtml += "</TR>";

        for(var i=0; i<items.length; i++){
            var item = items[i];

            var seq = item.getAttribute("seq");
            var uname = item.getElementsByTagName("uname")[0].firstChild.nodeValue;
            var title = item.getElementsByTagName("btitle")[0].firstChild.nodeValue;
            var commentcnt = item.getElementsByTagName("bcommentcnt")[0].firstChild.nodeValue;
            var viewcnt = item.getElementsByTagName("bviewcnt")[0].firstChild.nodeValue;
            var createtime = item.getElementsByTagName("bcreatetime")[0].firstChild.nodeValue;
            uname = toEntity(uname);
            title = toEntity(title);

            outhtml += "<TR>";
            outhtml += "  <TD class='list_td' align='center'>"+seq+"</TD>";
            outhtml += "  <TD class='list_td'><A onclick='act_view("+seq+");'>"+title+"</A> ("+commentcnt+")</TD>";
            outhtml += "  <TD class='list_td' align='center'>"+uname+"</TD>";
            outhtml += "  <TD class='list_td' align='center'>"+toDate(createtime)+"</TD>";
            outhtml += "  <TD class='list_td' align='center'>"+viewcnt+"</TD>";
            outhtml += "</TR>";
        }

        var totalcnt = parseInt(lists.getAttribute("totalcnt"), 10);

        outhtml += func_paging(totalcnt);
    }else{
        outhtml += "<TR>";
        outhtml += "  <TD align='center'><B>There is none data.</B></TD>";
        outhtml += "</TR>";
    }

    outhtml += "</TABLE>";
    output.innerHTML = outhtml;
}

What does that I do a mistake, an error occurs?

Epenbarha
  • 3
  • 4
  • can you paste the stack trace – Ryan Nov 24 '14 at 06:56
  • One problem that comes to mind looking at your code is that you use string concatenation to create the XML document. That is prone to errors. Instead, use an XML library to create the XML. Existing Q&A material (actually sort of a reference question) that shows how to do that for example is: [How to generate XML file dynamically using PHP?](http://stackoverflow.com/q/486757/367456). – hakre Nov 24 '14 at 13:22
  • This error occurs. Uncaught TypeError: Cannot read property 'nodeValue' of null And this is the content of the XML. im hello 2 2014-11-25 01:44:28 letsdothis test it's a test content 1 2014-11-24 22:02:01 – Epenbarha Nov 25 '14 at 01:32

0 Answers0