Witam, mym problemem jest to, że nie wiem jak odczytać dane z pliku .json, konkretnie jak sprawdzić czy podany login przez użytkownika nie został już zapisany, czy nie ma go w bazie ( w tym json'ie ). Konkretnie chodzi mi o funkcje, która to sprawdzi.
Np. taki plik json
{
"imie":"Olga",
"nazwisko":"Bolka"
,"adres":"Gdzieś 41",
"kod_pocztowy":"12-345",
"email":"log@log.com",
"login":"log@log.com",
"password":"klhjfgsda"
}
Formularz rejestracji
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="icon" href="img/favicon.png" type="image/png">
<title>Rejestracja</title>
<!-- <link rel="stylesheet" href="css/styles.css">
<link rel="stylesheet" href="css/skeleton.css"> -->
</head>
<style>
.box {
width: 500px;
padding: 20px;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
background: #e0e0e0;
text-align: center;
}
.box h1 {
color: rgb(31, 2, 2);
text-transform: uppercase;
font-weight: 500;
}
.box input[type="email"],
.box input[type="text"],
.box input[type="date"],
.box input[type="text"],
.box input[type="password"] {
border: 0;
background: none;
display: block;
margin: 1px auto;
text-align: center;
border: 2px solid #b5dd5a;
padding: 4px 10px;
width: 200px;
outline: none;
color: rgb(0, 0, 0);
border-radius: 24px;
transition: 0.25s;
}
.box input[type="email"]:focus,
.box input[type="text"]:focus,
.box input[type="date"]:focus,
.box input[type="password"]:focus {
width: 280px;
border-color: #2ecc71;
}
.box input[type="submit"],
.box input[type="button"] {
border: 0;
background: none;
display: block;
margin: 10px auto;
text-align: center;
border: 2px solid #b5dd5a;
padding: 2px 30px;
outline: none;
color: rgb(7, 0, 0);
border-radius: 24px;
transition: 0.25s;
cursor: pointer;
}
.box input[type="submit"]:hover,
.box input[type="button"]:hover {
background: #2ecc71;
}
</style>
<body>
<div>
<center>
<form class="box" action="index.html" method="post">
<h1>Rejestracja</h1>
<input type="text" name="" placeholder="Imie" id="imie"><br><br>
<input type="text" name="" placeholder="Nazwisko" id="nazwisko"><br><br>
<input type="text" name="" placeholder="Adres zamieszkania" id="adres"><br><br>
<input type="text" name="" placeholder="Kod pocztowy" id="kod"><br><br>
<input type="email" name="" placeholder="Email" id="login"><br><br>
<input type="password" name="" placeholder="Hasło" id="haslo"><br><br>
<input type="password" name="" placeholder="Powtórz hasło" id="haslo2"><br><br>
<input type="submit" value="Zarejestruj" id="loguj">
</form>
</center><br>
</div>
</body>
</html>
JS:
var log = document.getElementById('loguj');
const logowanie = e => {
var imie = document.getElementById('imie').value;
var nazwisko = document.getElementById('nazwisko').value;
var adres = document.getElementById('adres').value;
var kod = document.getElementById('kod').value;
var email = document.getElementById('login').value;
var login = document.getElementById('login').value;
var password = document.getElementById('haslo').value;
var password2 = document.getElementById('haslo2').value;
var user1 = {
imie: imie,
nazwisko: nazwisko,
adres: adres,
kod_pocztowy: kod,
email: email,
login: login,
password: password
}
console.log(user1);
var JSONuser = JSON.stringify(user1);
console.log(JSONuser);
if (login.length > 5 && password.length >= 8 && password2.length >= 8) {
if (imie.length > 3 && nazwisko.length < 3 && kod.length == 6) {
alert("Zarejestrowano");
}
} else alert("Błędne dane");
function saveJSON(jsonObject, filePath, append = false) {
const xhr = new XMLHttpRequest();
xhr.addEventListener("load", function() {
if (xhr.status !== 200) {
console.log("Błąd komunikacji z php");
}
});
xhr.open("POST", encodeURI("saveJSON.php?append=" + append + "&filepath=" + filePath), true);
xhr.setRequestHeader("Content-type", "application/json");
xhr.send(JSON.stringify(jsonObject));
}
function getJSON(filePath) {
const xhr = new XMLHttpRequest();
xhr.open("GET", filePath, false);
xhr.setRequestHeader("Content-type", "application/json");
xhr.send();
if (xhr.status !== 200) {
return null;
} else {
return JSON.parse(xhr.response);
}
}
//Odczyt danych
var config = getJSON("db/config.json");
console.log(config);
//Funkcja pobierania
var data = getJSON('db/users.json', function(e) {
})
saveJSON(user1, config.database.users, true);
// saveJSON(user3, config.database.users, true);
}
loguj.addEventListener('click', logowanie)
const config = {
filepath: "db/config.json",
name: "Niezwykły sklep",
addres: "http://adres mojej strony na serwerze",
database: {
config: "db/config.json",
users: "db/users.json",
products: "db/products.json",
orders: "db/orders.json",
},
interface: {
defaultSort: "name",
defaultItems: 16,
defaultLang: "pl"
}
}
<?php
if ($_GET['append'] == 'true') {
$data = json_decode(file_get_contents($_GET['filepath']));
if( !is_array($data) )
$json[] = $data;
else
$json = $data;
$json[] = json_decode( file_get_contents("php://input") );
file_put_contents($_GET['filepath'], json_encode($json) );
}
else
file_put_contents($_GET['filepath'], file_get_contents("php://input"));
if (!file_exists($_GET['filepath']))
echo 'Blad zapisu pliku';
else
echo 'OK';
?>