vovaJsc, вам нужно исправить ошибки... Отправляйте (да, нужно отправить!) на сервер асинхронно...
php.js
let person = {
name:"Vova",
last:"Nak",
age:18
}
function toPHP(){
var xhr = new XMLHttpRequest();
xhr.open("POST", "php1.php");
xhr.setRequestHeader("Content-Type", "application/json");
xhr.send(JSON.stringify(person));
xhr.onreadystatechange = function(){
if(xhr.readyState != 4) return;
if(xhr.status != 200){
console.log(xhr.status + ": " + xhr.statusText);
} else{
console.log("Hello", xhr.responseText);
}
}
}
document.getElementById("goP").addEventListener("click",toPHP)
Скрипт на PHP обычно начинается с <?php и у вас синтаксическая ошибка...
Для примера, отправляет то, что принято!
php1.php
<?php
$a = json_decode(file_get_contents('php://input'));
if($_SERVER['REQUEST_METHOD'] == 'POST') {
echo json_encode($a);
}
laimas, а экранировать кто будет?