[英]loop through JSON file using ajax, PHP & Javascript
I'm creating a small game where users must register or login before playing. 我正在创建一个小游戏,用户在玩之前必须先注册或登录。 I have a separate json file that stores already registered users.
我有一个单独的json文件,用于存储已注册的用户。
Once a user enters their username and password into a field I make an AJAX call to retrieve the data using PHP with the intent of checking whether their details are on file. 用户在字段中输入用户名和密码后,我将进行AJAX调用以使用PHP检索数据,以检查其详细信息是否在文件中。 Firstly I tried sending back a JSON encoded object to parse through in Javascript.
首先,我尝试发送回JSON编码的对象以在Javascript中进行解析。 This is the code I have so far:
这是我到目前为止的代码:
JSON: JSON:
{"LogIns":[
{
"Username":"mikehene",
"password":"123"
},
{
"Username":"mike",
"password":"123"
}
]
}
HTML: HTML:
<fieldset>
<legend>Please log in before playing</legend>
<form>
Username: <br>
<input type="text" placeholder="Enter a Username" id="username1" name="username"><br>
Password: <br>
<input type="password" placeholder="Enter a password" id="password" name="password"><br>
<input type="submit" value="Submit" onclick="return checkLogin();">
</form>
</fieldset>
PHP: PHP:
<?php
$username = $_POST['username'];
$str = file_get_contents('logins.json'); // Save contents of file into a variable
$json = json_decode($str, true); // decode the data and set it to recieve data asynchronosly - store in $json
echo json_encode($json);
?>
Javascript & AJAX call: JavaScript和AJAX调用:
var usernamePassed = '';
function checkLogin(){
usernamePassed = document.getElementById("username1").value;
callAJAX();
return false;
}
function callAJAX(){
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange=function() {
if (xhttp.readyState == 4 && xhttp.status == 200) {
myFunction(xhttp.responseText);
}
}
xhttp.open("POST", "LogInReg.php", true);
xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhttp.send("username=" + usernamePassed);
}
function myFunction(response) {
var arr = response;
var objJSON = JSON.parse(arr);
var len = objJSON.length;
for(var key in objJSON){
console.log(key);
}
}
But it only prints out "LogIns". 但是它仅打印出“ LogIns”。 I also tried this:
我也试过这个:
for (var i = 0; i < objJSON.length; ++i) {
if(objJSON[0].Username == usernamePassed){
console.log("found it");
}
else{
console.log("didn't find it!");
}
}
Therefore I tried another approach (parse the data in the PHP file) like so: 因此,我尝试了另一种方法(解析PHP文件中的数据),如下所示:
foreach ($json['LogIns'][0] as $field => $value) {
if($json['LogIns'][0]['Username'] == $username){
echo "Logged In";
break;
}
else{
echo "No user found";
break;
}
}
But when I enter "mike" as a user name it is echoing "No user found". 但是,当我输入“ mike”作为用户名时,它会回显“找不到用户”。 So I'm lost!
所以我迷路了! I'm new to coding and trying to learn myself.
我是编码和尝试自我学习的新手。 I would love to learn how to do it both methods (ie PHP and Javascript).
我很想学习如何同时使用这两种方法(即PHP和Javascript)。
Everything I've found online seems to push toward JQuery but I'm not quite comfortable/good enough at JQuery yet so would like to gradually work my way up to that. 我在网上找到的所有内容似乎都在朝着JQuery迈进,但我对JQuery不太满意/不够好,因此希望逐步实现这一目标。
I haven't even got to the register a user yet where I'm going to have to append another username and password on registration. 我什至还没有注册用户,在注册时我将不得不追加另一个用户名和密码。
Any help would be GREATLY appreciated. 任何帮助将不胜感激。
Thanks in advance 提前致谢
Try this 尝试这个
$json = json_decode($str, true);
$password = $_POST['password'];
foreach($json['LogIns'] as $res)
{
if($res['Username']==$username && $res['password']==$password)
{
echo json_encode($res['Username']);
//echo 'user found';
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.