When I click on the button, I want to load the content. Just do not know in what form the php script should return the answer. Js:

function load() { xhr.open('POST', './script.php', true); xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=utf-8"); xhr.onreadystatechange = function () { if (xhr.readyState == 4 && xhr.status == 200) { var elems = xhr.responseText; document.querySelector(".content").appendChild(elems); } } xhr.send("load=true$from="+fact_id+"&to="+fact_id+1); } 

PHP:

 if(isset($_POST['load'])) { $from = $_POST['from']; $to = $_POST['to']; $q = mysql_query("SELECT * FROM puzzles LIMIT $from, $to "); while(($row=mysql_fetch_assoc($q))!=false){ echo '<div>'; echo $row['description']; echo '</div>'; } 

With this code there will be an error: Uncaught TypeError: Failed to execute 'appendChild' on 'Node': parameter 1 is not of type 'Node' . What data should I return in response?

    1 answer 1

    The problem is not on the php side, but in the js code. The appendChild method expects Node , and gets a string, you need to change the code a little, like this:

     if (xhr.readyState == 4 && xhr.status == 200) { var response = document.createElement('div'); response.innerHTML = xhr.responseText; document.querySelector(".content").appendChild(response); }