JSON формат в php для отправки ajax -ом в JS
Задача создавать на php json формат и посылать его в html документ с javascript . Опять обыскал весь интернет, но нет достаточного пояснения, как. В php записываю массив
<?php
$voting="voting.json";
$stack=array();
$stack['vo']="v";
$stack['no']="n";
$stack['excellent']="w";
class voting{
public $vo;
public $no;
public $excellent;
}
$vot = new voting;
// $vot->$vo=+1;
// printf($vot->$vo);
$ip=$_SERVER['REMOTE_ADDR'];
$urlRefreshA="voting.html";
// array_push($stack,$vot->$vo,$ip);
if(isset($_POST['VO'])){
// echo "privet";
if(file_exists($voting)){
// $vot=file_get_contents($voting);
// $object = new ArrayObject($stack);
// $sta=array_push($st," GOOD _");
// file_put_contents('file.json',$st);// perezapisyvaet file
// array_count_values($stack);
// $novue=json_encode($st);
// $stack=['vo'=>['1']];
// $text= join(" ",file(voting.json));
$text=file_get_contents($voting);
// шаблон для поиска
$template = "/[vwn]/";
$template_v="/[v]/";
$template_w="/[w]/";
$template_n="/[n]/";
// производим поиск
$result = preg_match_all($template, $text,$maches);
echo "Совпадений: $result<br>";
// var_dump($result);
$result_V = preg_match_all($template_v, $text,$maches);
echo "ХОРОШО: $result_V<br>";
// var_dump($result_V);
$result_w = preg_match_all($template_w, $text,$maches);
echo "ОТЛИЧНО: $result_w<br>";
// var_dump($result_w);
$result_n = preg_match_all($template_n, $text,$maches);
echo ":-( НО: $result_n<br>";
// var_dump($result_n);
$stack=['vo'=>'odyn'];
$two=json_encode($stack);
// echo count($stack['vo']);
file_put_contents($voting, $two, FILE_APPEND | LOCK_EX);
}else{
file_get_contents($voting);
$stack=['vo'=>'onyn'];
$two=json_encode($stack);
echo "<pre>";
echo "</pre>";
file_put_contents($voting,$two, FILE_APPEND | LOCK_EX);
}
header("refresh:1; url=/$urlRefreshA");
}else if(isset($_POST['NO'])){
echo "Yy buvaet";
if(file_exists($voting)){
file_get_contents($voting);
// $stack=['NO'];
$text=file_get_contents($voting);
// шаблон для поиска
$template = "/[vwn]/";
$template_v="/[v]/";
$template_w="/[w]/";
$template_n="/[n]/";
// производим поиск
$result = preg_match_all($template, $text,$maches);
echo "Совпадений: $result<br>";
// var_dump($result);
$result_V = preg_match_all($template_v, $text,$maches);
echo "ХОРОШО: $result_V<br>";
// var_dump($result_V);
$result_w = preg_match_all($template_w, $text,$maches);
echo "ОТЛИЧНО: $result_w<br>";
// var_dump($result_w);
$result_n = preg_match_all($template_n, $text,$maches);
echo ":-( НО: $result_n<br>";
// var_dump($result_n);
$stack=$stack['no'];
$two=json_encode($stack,true);
echo "<pre>";
echo "<br>";
var_dump($two);
echo "</pre>";
echo count($two);
file_put_contents($voting, $two, FILE_APPEND | LOCK_EX);
}else{
file_get_contents($voting);
$stack=$stack['no'];
$two=json_encode($stack);
echo "<pre>";
var_dump($two);
echo "</pre>";
file_put_contents($voting,$two, FILE_APPEND | LOCK_EX);
}
header("refresh:3; url=/$urlRefreshA");
}else if(isset($_POST['excellent'])){
if(file_exists($voting)){
file_get_contents($voting);
$text=file_get_contents($voting);
// шаблон для поиска
$template = "/[vwn]/";
$template_v="/[v]/";
$template_w="/[w]/";
$template_n="/[n]/";
// производим поиск
$result = preg_match_all($template, $text,$maches);
echo "Совпадений: $result<br>";
// var_dump($result);
$result_V = preg_match_all($template_v, $text,$maches);
echo "ХОРОШО: $result_V<br>";
// var_dump($result_V);
$result_w = preg_match_all($template_w, $text,$maches);
echo "ОТЛИЧНО: $result_w<br>";
// var_dump($result_w);
$result_n = preg_match_all($template_n, $text,$maches);
echo ":-( НО: $result_n<br>";
// var_dump($result_n);
$stack=$stack['excellent'];
$two=json_encode($stack,true);
echo "<pre>";
var_dump($two);
echo "</pre>";
echo count($two);
file_put_contents($voting, $two, FILE_APPEND | LOCK_EX);
}else{
file_get_contents($voting);
$stack=$stack['excellent'];
$two=json_encode($stack);
echo "<pre>";
var_dump($two);
echo "</pre>";
file_put_contents($voting,$two, FILE_APPEND | LOCK_EX);
}
header("refresh:1; url=/$urlRefreshA");
}else{
echo "who!";
}
?>
<!DOCTYPE html>
<html lang="ru" dir="ltr">
<head>
<meta charset="utf-8">
<title>Скрипт голосовалки.</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="canonical" href="/voting.php">
<meta name="robots" content="all">
<meta name="autor" content="Amir Navrutdinov">
<link rel="icon" href="images/icon9.svg">
<meta name="theme-color" content="#a0ffed">
<style>
*{
margin:0;
}
body{
background: green;
min-width: 100vh;
max-width: 100%;
}
main{
display:flex;
justify-content: center;
align-items: center;
max-width: 100%;
min-height: 100vh;
max-height: 100%;
}
h1{
max-width: 100%;
}
</style>
</head>
<body>
<main>
<h1>Вот Такие ДЕЛА!<?= $ip ?></h1>
</main>
</body>
</html>
<!DOCTYPE html>
<html lang="ru" dir="ltr">
<head>
<meta charset="utf-8">
<title></title>
<meta name="description" content="">
<!-- VAZhnoE -->
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="canonical" href="/voting.html">
<meta name="robots" content="all">
<meta name="autor" content="Amir Navrutdinov">
<link rel="icon" href="images/icon9.svg">
<meta name="theme-color" content="#a0ffed">
</head>
<body>
<form action="generation-objects/voting.php" method="post">
<button name="excellent" style="background:none; border:none;font-size:75px;">?
</button>
<!-- <button name="result">result</button> -->
<button name="VO" style="background:none; border:none;font-size:75px;">?</button>
<button name="NO" style="background:none; border:none;font-size:75px;">?</button>
<!-- <button name="OO" style="background:none; border:none;font-size:75px;">?
</button> -->
</form>
</body>
<script>
function readTextFile(file, callback) {
let rawFile = new XMLHttpRequest();
rawFile.overrideMimeType("application/json");
rawFile.open("GET", file, true);
rawFile.onreadystatechange = function() {
if (rawFile.readyState === 4 && rawFile.status == "200") {
callback(rawFile.responseText);
}
}
rawFile.send(null);
}
//usage:
readTextFile("generation-objects/voting.json", function(text){
let data = JSON.parse(text);
console.log(Object.values(data));
document.getElementById('text').innerHTML=Object.values(data).join("");
});
</script>
</html>
Подскажите пожалуйста где копать чтобы проверить взаимодействие js с помощью json на сервере php . Заранее большое спасибо! Успешного дня, удачи, и волшебного настроения! :-D