I can't seem to figure this out and I've tried everything. I want a form to submit my data as a JSON, and then head to a PHP page, where it outputs the results from the JSON data.
I have it set as the following but this does absolutely nothing.
Form code:
<form name="login" onsubmit="SendForm();">
<input class="textbox" type="text" name="username" placeholder="Username"><p>
<input class="textbox" type="password" name="password" placeholder="Password"><p>
<br><input class="submit" type="submit" value="Log in!">
</form>
Send Form:
function SendForm() {
var username = document.login.username.value;
var password = document.login.password.value;
var xhr = new XMLHttpRequest();
xhr.open("POST", "/");
xhr.setRequestHeader('Content-Type', 'application/json; charset=utf-8');
var postData = {
object: { child1: username, child2: password }
}
xhr.send(postData);
return true;
}
PHP code to read it:
<?php
$json = @file_get_contents('php://input');
$array = json_decode($json, true);
if (empty($array))
echo 'empty';
else
print_r($array);
?>
Any help at all would be great. Thanks!