Update index.php
Browse files
index.php
CHANGED
@@ -1,61 +1,74 @@
|
|
1 |
-
<?php
|
2 |
-
// Define the file path
|
3 |
-
$file = 'user.json';
|
4 |
-
|
5 |
-
// Check if the form is submitted
|
6 |
-
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
7 |
-
// Get the username and password from the form
|
8 |
-
$username = $_POST['username'];
|
9 |
-
$password = $_POST['password'];
|
10 |
-
|
11 |
-
// Create an array with the username and password
|
12 |
-
$data = array(
|
13 |
-
'username' => $username,
|
14 |
-
'password' => $password
|
15 |
-
);
|
16 |
-
|
17 |
-
// Read the existing JSON data from the file
|
18 |
-
$jsonData = file_get_contents($file);
|
19 |
-
|
20 |
-
// Convert the JSON data to an array
|
21 |
-
$existingData = json_decode($jsonData, true);
|
22 |
-
|
23 |
-
// Add the new data to the existing array
|
24 |
-
$existingData[] = $data;
|
25 |
-
|
26 |
-
// Convert the array to JSON format
|
27 |
-
$json = json_encode($existingData);
|
28 |
-
|
29 |
-
// Save the JSON data to the file
|
30 |
-
file_put_contents($file, $json);
|
31 |
-
|
32 |
-
// Display a success message
|
33 |
-
echo 'Username and password saved successfully!';
|
34 |
-
}
|
35 |
-
|
36 |
-
// Read the JSON data from the file
|
37 |
-
$jsonData = file_get_contents($file);
|
38 |
-
|
39 |
-
// Convert the JSON data to an array
|
40 |
-
$dataArray = json_decode($jsonData, true);
|
41 |
-
?>
|
42 |
-
|
43 |
-
<!DOCTYPE html>
|
44 |
-
<html>
|
45 |
-
<head>
|
46 |
-
<title>Save User</title>
|
47 |
-
</head>
|
48 |
-
<
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
</
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
61 |
</html>
|
|
|
1 |
+
<?php
|
2 |
+
// Define the file path
|
3 |
+
$file = 'user.json';
|
4 |
+
|
5 |
+
// Check if the form is submitted
|
6 |
+
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
7 |
+
// Get the username and password from the form
|
8 |
+
$username = $_POST['username'];
|
9 |
+
$password = $_POST['password'];
|
10 |
+
|
11 |
+
// Create an array with the username and password
|
12 |
+
$data = array(
|
13 |
+
'username' => $username,
|
14 |
+
'password' => $password
|
15 |
+
);
|
16 |
+
|
17 |
+
// Read the existing JSON data from the file
|
18 |
+
$jsonData = file_get_contents($file);
|
19 |
+
|
20 |
+
// Convert the JSON data to an array
|
21 |
+
$existingData = json_decode($jsonData, true);
|
22 |
+
|
23 |
+
// Add the new data to the existing array
|
24 |
+
$existingData[] = $data;
|
25 |
+
|
26 |
+
// Convert the array to JSON format
|
27 |
+
$json = json_encode($existingData);
|
28 |
+
|
29 |
+
// Save the JSON data to the file
|
30 |
+
file_put_contents($file, $json);
|
31 |
+
|
32 |
+
// Display a success message
|
33 |
+
echo 'Username and password saved successfully!';
|
34 |
+
}
|
35 |
+
|
36 |
+
// Read the JSON data from the file
|
37 |
+
$jsonData = file_get_contents($file);
|
38 |
+
|
39 |
+
// Convert the JSON data to an array
|
40 |
+
$dataArray = json_decode($jsonData, true);
|
41 |
+
?>
|
42 |
+
|
43 |
+
<!DOCTYPE html>
|
44 |
+
<html>
|
45 |
+
<head>
|
46 |
+
<title>Save User</title>
|
47 |
+
</head>
|
48 |
+
<title>User JSON Display</title>
|
49 |
+
<script>
|
50 |
+
// Fetch the user.json file
|
51 |
+
fetch('user.json')
|
52 |
+
.then(response => response.json())
|
53 |
+
.then(data => {
|
54 |
+
// Display the JSON data
|
55 |
+
document.getElementById('user-data').textContent = JSON.stringify(data, null, 2);
|
56 |
+
})
|
57 |
+
.catch(error => {
|
58 |
+
console.error('Error:', error);
|
59 |
+
});
|
60 |
+
</script>
|
61 |
+
</head>
|
62 |
+
|
63 |
+
<body>
|
64 |
+
<pre id="user-data"></pre>
|
65 |
+
<h2>Save User</h2>
|
66 |
+
<form method="POST" action="">
|
67 |
+
<label for="username">Username:</label>
|
68 |
+
<input type="text" name="username" id="username" required><br><br>
|
69 |
+
<label for="password">Password:</label>
|
70 |
+
<input type="password" name="password" id="password" required><br><br>
|
71 |
+
<input type="submit" value="Save">
|
72 |
+
</form>
|
73 |
+
</body>
|
74 |
</html>
|