【发布时间】:2017-07-07 14:27:51
【问题描述】:
我们正在尝试从前端表单向 Json 文件添加一些新用户。
我们通过 Js 传递从表单中获取的代码,然后我们尝试(通过 PHP)将用户插入的值传递到 json 文件中。
从 HTML 中提取
<div class="log">
<p>Username Nuovo Utente</p>
<input class="nome" id="nuovoUtente" placeholder="inserisci il nome"><br>
<p>Password Password Nuovo Utente</p>
<input type="password" id="nuovaPsw" class="psw"> <br>
<button type="submit" class="lo" id="aggiunto">Aggiungi Nuovo Utente</button>
</div>
从js文件中提取
let newNom = document.getElementById('nuovoUtente');
let newPass = document.getElementById('nuovaPsw');
aggiunto.onclick=function(){
$.get("utenti.php", { nome: newNom.value , pw: newPass.value });
}
php 文件
<?php
$data[] = $_GET['data'];
console.log($data[]);
$inp = file_get_contents('pindex.json');
$tempArray = json_decode($inp);
array_push($tempArray, $data);
$jsonData = json_encode($tempArray);
file_put_contents('pindex.json', $jsonData);
?>
【问题讨论】:
-
问题/问题是什么?
-
'$.get("utenti.php", { nome: newNom.value , pw: newPass.value });` 为了您自己和他人的安全,请不要 i> 通过 GET 发送密码
-
这些是标签吗?
标签: javascript php jquery json