0

I hav read so many articles on how to access the key values of an xml array but none is helping me.

Here is my issue,

I have a JavaScript code that keeps checking if there is an active timer session in the db after every second. There is a session.php script that fetches the active sessions and returns an array id's of active rows.

This is the array that is returned by session.php

Array ( [0] => response77011 [1] => response110022 )

I converted this array to xml array using the following code (this code is in session.php)

$array = $responses; ($response is an array from a db fetched using php)

//This function create a xml object with element document.
$arr = array_flip($array);
$xml = new SimpleXMLElement('<document/>');
array_walk_recursive($arr, array ($xml,'addChild'));
print $xml->asXML();

on my index.php page, i have the following code

setInterval(function() {

    
    var xmlhttp = new XMLHttpRequest();
    xmlhttp.open("GET","session.php",false);
    xmlhttp.send(null);
    y = xmlhttp.responseText;
    console.log(y);

    //var parser = new DOMParser();
   // var xmlDoc = parser.parseFromString(y.responseText, "text/xml");

   
    //If the array contains multiple ids,I need to loop them to show in the id attribute ot the 'td' of my   //table

    var xmlhttp = new XMLHttpRequest();
    xmlhttp.open("GET","response.php",false);
    xmlhttp.send(null);

    document.getElementById(y).innerHTML=xmlhttp.responseText;
    console.log(xmlhttp.responseText);
}, 1000);

The above code gives the following output on the console

<?xml version="1.0"?>
<document><0>response77011</0><1>response110022</1></document> 

( the above is console.log(y): where y is an array from session.php )

I want to loop through the array so that i get the value 'response77011' or 'response110022' and any other value in the array.

in each loop, I would like to display it in an id on a 'td' table element.

how can I achieve this please, it has really gnawed on me.

6
  • 2
    Why in the world are you using XML? Commented Sep 13, 2023 at 20:35
  • Why did you comment out the code that parses the XML? y is not an element ID, it's the complete XML, why do you use document.getElememtById(y)? Commented Sep 13, 2023 at 20:36
  • @Pointy you have a better solution? Commented Sep 14, 2023 at 9:45
  • If you don't actually need XML for some other reason, you can use JSON for simple data like that. Commented Sep 14, 2023 at 12:07
  • @Barmar that's you are right, but that is not the problem sir. When i parsed the response text, i wasnt sure what to do next Commented Sep 14, 2023 at 16:02

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.