Why do I always get undefined response with this ajax post to php?

Uros

I am using mouseover event on span element to initiate an ajax post call to php page, but I always get undefined, first for responseText when I used a simple echo to get response and now when I use responseXML. Can somebody please explain me why. Here is ajax code:

var span = document.getElementsByTagName('span');

for (var i = 0; i < span.length; i++) {
   span[i].addEventListener("mouseover", showInformation, false);
}

function showInformation(event) {
    var xhr = new XMLHttpRequest();

    xhr.open("POST", "../includes/ajax_response.php", true);
xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

xhr.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
        content(xhr, event);
    }
};

xhr.send("uname=" + event.target.firstChild.textContent);
}

function content(xhr, event) {
    var info = document.getElementById('displayInformation');
    var xmlResponse = xhr.resopnseXML;
    var xmlDocumentElement = xmlResponse.documentElement;
    var message = xmlDocumentElement.firstChild.data;
    info.innerHTML = message;
    info.style.visibility = "visible";
    event.target.addEventListener("mouseout", function() {
    document.getElementById('displayInformation').style.visibility = "hidden";
    }, false);
}

And this is php code:

$username = $_POST['uname'];
$query = "SELECT id, joined FROM users WHERE username = '{$username}' LIMIT 1";
$first_result = Database::getInstance()->query($query);
if ($first_result->num_rows == 1) {
    foreach ($first_result as $first) {
        $id = $first['id'];
        $joined = $first['joined'];
    }
}
$first_result->free();

$query = "SELECT COUNT(message) AS count FROM blogs WHERE user_id = '{$id}'";
$results = Database::getInstance()->query($query);
if ($results) {
    foreach ($results as $result) {
        $number = $result['count'];
    }
}

header("Content-Type: text/xml");
echo '<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>';
echo '<response>';
        echo "joined: {$joined}";
        echo "number of posts: {$number}";
echo '</response>';

This is version of php with xml, I tired simpler versions with just $username = $_POST['uname'] and then echo $username, but always response is undefined.

nvkrj

I read your code once and there does not seem to be any major error. However, I found out this minor bug:

var xmlResponse = xhr.resopnseXML;

spelling of responseXML is incorrect .. maybe that's what's causing the xmlResponse to be undefined?

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Why do i always get NULL in php response when i try to send some variable using ajax

Why do I get undefined for response.name?

Ajax CORS always fails although I do get a valid response from server

Why do I get "Undefined variable" in my custom PHP function

Why my data get undefined when I successfully get the response?

React 17: Why do I have to send a post request twice to get a valid or error response?

Why do I get scrapy response empty?

Ajax Get response data undefined

Why do I get a 'constructor is undefined` error?

Why do I get property 'map' is undefined

why do i get undefined in the console in JavaScript

Why do I get undefined plus the value?

Why do I get property of undefined error?

Why do I get undefined here?

Why do I get undefined variable in an if statement?

Why do i get QJsonValue(undefined)?

Why do I not get the response as RAW API RESPONSE specified by Dialogflow?

Why is php session data not saved when I post data at almost the same time an ajax get request is executed?

Undefined Index in Ajax POST or PHP

How do I change GET method to POST method using php and ajax

How do I get the correct php array format for WordPress Ajax $_POST data?

Why do I get an undefined variable error when trying to use PHP in two sections of a page?

Why do I get the error Call to undefined function even with include_once?(php)

Jquery Post PHP always sending Success Response

Why i get the notice "Undefined index" if they are in the url post

PHP and Ajax does not always return a response

why ajax value return is undefined with settimeout? how do I fix it?

how do i get a response from a jquery ajax request

How do I get a response from a post request if the response is different from the post class