Dooratre/php-test
0
1<?php2// Define the file path3$file = 'user.json';4 5// Check if the form is submitted6if ($_SERVER['REQUEST_METHOD'] === 'POST') {7 // Get the username and password from the form8 $username = $_POST['username'];9 $password = $_POST['password'];10 11 // Create an array with the username and password12 $data = array(13 'username' => $username,14 'password' => $password15 );16 17 // Read the existing JSON data from the file18 $jsonData = file_get_contents($file);19 20 // Convert the JSON data to an array21 $existingData = json_decode($jsonData, true);22 23 // Add the new data to the existing array24 $existingData[] = $data;25 26 // Convert the array to JSON format27 $json = json_encode($existingData);28 29 // Save the JSON data to the file30 file_put_contents($file, $json);31 32 // Display a success message33 echo 'Username and password saved successfully!';34}35 36// Read the JSON data from the file37$jsonData = file_get_contents($file);38 39// Convert the JSON data to an array40$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 file51 fetch('user.json')52 .then(response => response.json())53 .then(data => {54 // Display the JSON data55 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>