//html
<!doctype html>
<html lang="ru">
<head>
<title>Document</title>
</head>
<body>
<div id="write" onclick = "getData();">saad</div>
<script type="text/javascript" src="script.js"></script>
</body>
</html>
//javascript
function getXMLHttpRequest() {
try {
var xmlhttp = new XMLHttpRequest();
}
catch(e1) {
try {
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
}
catch(e2) {
try {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e3) {
xmlhttp = false;
}
}
}
return xmlhttp;
};
function getData() {
var xmlhttp = getXMLHttpRequest();
xmlhttp.open("GET", "script.php", true);
xmlhttp.send();
xmlhttp.onreadystatechange = function() {
if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById("write").innerHTML = xmlhttp.responseText;
}
}
};
//php
require_once "sql.php";
$result = mysqli_query($link, "SELECT *FROM text");
while($row = mysqli_fetch_row($result)) {
echo $row[1];
}
mysqli_free_result($result);
mysqli_close($link);
//php_sql
$link = mysqli_connect("localhost", "root", "" ,"blog");
if(mysqli_connect_errno()) {
printf("Не удалось подключиться: %s\n",mysqli_connect_error());
exit;
}
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (xhr.readyState == 4){
document.getElementById('response').innerHTML = xhr.responseText;
var responseArray = JSON.parse(xhr.responseText);
}
};
xhr.open("POST", "php/myRequest.php");
xhr.send();
<?php
header("Content-type: text/txt; charset=UTF-8");
$link = mysqli_connect('localhost', 'root', '', 'myDB');
if (!$link) {
echo('Ошибка соединения');
}
$result = mysqli_query($link, 'select * from myTable');
$resultArray = array();
while ($line = mysqli_fetch_array($result, MYSQLI_NUM)) {
$resultArray[] = $line;
}
$result->close();
$res = json_encode($resultArray, JSON_UNESCAPED_UNICODE);
echo $res;
$link->close();
?>
$.ajax(params).then(function(mysql_data){
console.log(mysql_data);
})