|
<?php |
|
|
|
$file = 'user.json'; |
|
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') { |
|
|
|
$username = $_POST['username']; |
|
$password = $_POST['password']; |
|
|
|
|
|
$data = array( |
|
'username' => $username, |
|
'password' => $password |
|
); |
|
|
|
|
|
$jsonData = file_get_contents($file); |
|
|
|
|
|
$existingData = json_decode($jsonData, true); |
|
|
|
|
|
$existingData[] = $data; |
|
|
|
|
|
$json = json_encode($existingData); |
|
|
|
|
|
file_put_contents($file, $json); |
|
|
|
|
|
echo 'Username and password saved successfully!'; |
|
} |
|
|
|
|
|
$jsonData = file_get_contents($file); |
|
|
|
|
|
$dataArray = json_decode($jsonData, true); |
|
?> |
|
|
|
<!DOCTYPE html> |
|
<html> |
|
<head> |
|
<title>Save User</title> |
|
</head> |
|
<title>User JSON Display</title> |
|
<script> |
|
|
|
fetch('user.json') |
|
.then(response => response.json()) |
|
.then(data => { |
|
// Display the JSON data |
|
document.getElementById('user-data').textContent = JSON.stringify(data, null, 2); |
|
}) |
|
.catch(error => { |
|
console.error('Error:', error); |
|
}); |
|
</script> |
|
</head> |
|
|
|
<body> |
|
<pre id="user-data"></pre> |
|
<h2>Save User</h2> |
|
<form method="POST" action=""> |
|
<label for="username">Username:</label> |
|
<input type="text" name="username" id="username" required><br><br> |
|
<label for="password">Password:</label> |
|
<input type="password" name="password" id="password" required><br><br> |
|
<input type="submit" value="Save"> |
|
</form> |
|
</body> |
|
</html> |