Treat AJAX return (responseText)

3

I made a code in AJAX PHP executes and prints in the HTML the result of the SQL query, the problem is that when it displays on the screen, the data is not treated, look like this:

WhatIneedissimplytodisplayonlythecontentsof'ayzac_episode_name',butIcannotlosetheotherinformation(mainly'ayzac_episode_id')

followthecodes:

AJAX:

functionsearchEpisode(seasonID,divepisodioID){if(window.XMLHttpRequest){req=newXMLHttpRequest();}elseif(window.ActiveXObject){req=newActiveXObject("Microsoft.XMLHTTP");
    }

var url = "../control/ayzac_control_getEpisode.php?id=" + seasonID;
req.open("Get", url, true);

req.onreadystatechange = function() {
    if (req.readyState == 4 && req.status == 200) {
        var answer = req.responseText;
        alert(answer);
        document.getElementById("episodios" + divepisodioID).innerHTML = answer;

    }
}

PHP (function that looks for the episode):

    function getSerieEpisodes($seasonID){
    $connect = new ControllerConnect ();
    $objCon = $connect->controllerConnect ();
    $sql = "SELECT * FROM ayzac_episode WHERE ayzac_season_id = '".$seasonID."'";
    return $objCon->executeSQLFetchObjectEpisodes($sql);
}

PHP (executeSQLFetchObjectEpisodes):

    function executeSQLFetchObjectEpisodes($sql = NULL) {
    if ($sql != NULL) {
        $query = mysqli_query ( $this->con, $sql );
        while ( $result = mysqli_fetch_object ( $query ) ) {
            $this->rows [] = $result;
        }
        if ($this->rows != null) {
            return $this->rows;
        }
        return false;
    } else {
        return false;
    }
}
    
asked by anonymous 05.10.2017 / 00:51

2 answers

0

This return you receive, an array, and if you store it in a variable, $ array, and to read the information ayzac_episode_name, you scan the array with a foreach, like this:

foreach($array->array as $a){
  echo $a->ayzac_episode_name;
}
    
05.10.2017 / 04:03
0

I was able to solve the problem in a much simpler way than I imagined.

I realized that the value that was being displayed was of the var_dump function that was present in the controller (which I did not post up there)

It was like this:

$objUser = new userMidia ();
$rowEpisode = $objUser->getSerieEpisodes( filter_input ( INPUT_GET, 'id' ) );
var_dump ($rowEpisode);

Now it looks like this:

$objUser = new userMidia ();
$rowEpisode = $objUser->getSerieEpisodes( filter_input ( INPUT_GET, 'id' ) );
$i=0;
echo "<ul>";
while(isset($rowEpisode[$i])){
    echo "<li>";
    echo $rowEpisode[$i]->ayzac_episode_name;
    echo "</li>";
    $i++;
}
echo "</ul>";

Thank you!

    
05.10.2017 / 14:40