Treat AJAX return (responseText)

Asked

Viewed 1,061 times

3

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

Imagem demonstrativa

What I need is simply that on the screen appear only the content of 'ayzac_episode_name', but I can not lose the other information (mainly 'ayzac_episode_id')

follow the codes:

AJAX:

function searchEpisode(seasonID,divepisodioID) {
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        req = new ActiveXObject("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 searches 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;
    }
}

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;
}

0

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

I realized that the value that was being displayed was from the var_dump function that was present in the controller (which I didn’t post up there)

It was like this:

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

Now it’s 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!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.