-4

I want to retrieve my database values and insert them into a table.

I am trying to iterate i onto an array.

However, it returns undefined and there is no output. Why is this so?

Could it be an issue with the script or the body tag?

Script

function getitemdetails() {
  var xmlhttp = new XMLHttpRequest();
  var url = serverURL() + "/mycasefeed.php";
  url += "?Case_ID=" + decodeURIComponent(getUrlVars()["Case_ID"]);
  xmlhttp.onreadystatechange = function() {
    if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
      getitemResult(xmlhttp.responseText);
    };
  }
  xmlhttp.open("GET", url, true);
  xmlhttp.send();
}

function getitemResult(response) {
  var arr = JSON.parse(response);
  var i;


    for(i = 0; i < arr.length; i++) {

    $("#mycase").append("<tr><td>" + arr[i].CaseDes +
    arr[i].Case_Pic +
    "<br>" + arr[i].categoryname +
    "<br>" + arr[i].Caselat +
    "<br>" + arr[i].Caselong +
    "<br>" + arr[i].CaseTime +
    "<br>" + arr[i].details + "</td></tr>");
}
$("#mycaseresult").table("refresh");
}

getitemdetails();

Body

<div id="SearchResult" class="ui-content">
  <table data-role="table" data-mode="reflow" class="ui-responsive" id="mycaseresult">
    <thead>
      <tr>
        *insert values here*
      </tr>
    </thead>
    <tbody id="mycase">

    </tbody>
  </table>
</div>
Zesty
  • 73
  • 8

2 Answers2

1

the reason it is returned undefined is because the variable has no content.

ca1c
  • 1,111
  • 10
  • 23
1

In javascript, any empty variable (declared, but not defined) is undefined.

var a;
var b = {};

function foo(parameter) {
    console.log(parameter);
}

console.log(a); // Outputs: `undefined`
console.log(b.cool); // Outputs: `undefined`
foo(); // Outputs: `undefined`

On your code, looks like you are trying to iterate on an array, but you don't have any iteration loop.

Try:

function getitemResult(response) {
  var arr = JSON.parse(response);
  for(var i=0; i < arr.length; i++) {
      $("#mycase").append("<tr><td>" + arr[i].CaseDes +
          arr[i].Case_Pic +
          "<br>" + arr[i].categoryname +
          "<br>" + arr[i].Caselat +
          "<br>" + arr[i].Caselong +
          "<br>" + arr[i].CaseTime +
          "<br>" + arr[i].details + "</td></tr>");
   }
}
Elias Soares
  • 9,884
  • 4
  • 29
  • 59