Skip to content

Instantly share code, notes, and snippets.

@webyroki
Created March 31, 2017 19:32
Show Gist options
  • Save webyroki/e2b7884511e18f494000b6f387fa53d2 to your computer and use it in GitHub Desktop.
Save webyroki/e2b7884511e18f494000b6f387fa53d2 to your computer and use it in GitHub Desktop.
Отправить данные из JS методом POST и сохранить в json
<?php
$file = 'index.json'; //куда сохраняем
if (file_get_contents("php://input")){
$postdata = file_get_contents("php://input");// читаем данные в строку в raw формате
file_put_contents($file, $postdata, LOCK_EX);
}
<!DOCTYPE html>
<html>
<body>
<h1>The XMLHttpRequest Object</h1>
<button type="button" onclick="loadDoc()">Отправить</button>
<p id="demo"></p>
<script>
var event = {
title: "Конференция",
date: "сегодня"
};
var str = JSON.stringify(event);
function loadDoc() {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("demo").innerHTML = this.responseText;
}
};
xhttp.open("POST", "forms.html.php", true);
xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhttp.send(str);
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment