将数据写入到从PHP文件的JSON文件 [英] Write data to a json file from PHP file
问题描述
我有一个test.php的网页,其中里显示3有3个添加链接按钮,在点击按钮,用户可以看到一个弹出窗口。在窗口,他添加了链接。一旦链接被添加,基地页面会从添加链接按钮,切换到超链接的新链接。现在,我要通过我从test.php的用户收到新的链接使用Ajax调用links.php。 Links.php必须有一个JSON code写的链接到名为first.json另一个文件。 first.jason将键值对的变量和链接。我将不得不再次找到从.json文件中的值,并重建到一个数组,更新相应的变量,保存回去。
I have a test.php page which displayes three has 3 "Add Link" buttons, on clicking the buttons the user sees a popup window. In the window he adds the link. Once the link is added , the base page will change from "Add link" button to hyperlink with the new link. Now, I have to pass the new link I receive from the user from test.php to links.php using an ajax call. Links.php has to have a JSON code to write the link to another file called first.json. first.jason will have key value pair for variable and link. I would have to retrieve the value from .json file later and reconstruct into an array, update the corresponding variable and save it back.
我到目前为止,设法从test.php的,并能够通过Ajax调用发送同样以links.php新的链接。我也能够显示我接收并验证了同样的链接。现在,我想复制链接到.json文件作为一个关键的谷对。我是新来的JSON和无法弄清楚如何去做。我的变量$ P,在links.php有联系。
I have by far, managed to get the new link from test.php and able to send the same via ajax call to links.php. I am also able to display the link I receive and have verified the same. Now, I would like to copy the link into .json file as a key vale pair. I am new to json and unable to figure out how to go about it. My variable $p, in links.php has the link.
在同任何指针会有所帮助。谢谢。
Any pointers on the same will be helpful. Thanks.
下面是我的code。在test.php的:
Below is my code in test.php:
<!DOCTYPE html>
<html>
<body>
<div id="demos1">
<button id="demo1" onclick="Link1()">Add Link-1</button>
<br>
</div>
<div id="demos2">
<button id="demo2" onclick="Link2()">Add Link-2</button>
<br>
</div>
<div id="demos3">
<button id="demo3" onclick="Link3()">Add Link-3</button>
<br>
</div>
<div id="txtHint"></div>
<script>
function Link1()
{
var demo1 = document.getElementById('demo1');
var demos1 = document.getElementById('demos1');
var value1 = prompt("Please Enter the Link");
var link1 = document.createElement('a');
link1.setAttribute('href', value1);
link1.innerHTML = "New Link1";
demo1.parentNode.removeChild(demo1);
demos1.appendChild(link1);
sendlink(value1);
}
function Link2()
{
var demo2 = document.getElementById('demo2');
var demos2 = document.getElementById('demos2');
var value2 = prompt("Please Enter the Link");
var link2 = document.createElement('a');
link2.setAttribute('href', value2);
link2.innerHTML = "New Link2";
demo2.parentNode.removeChild(demo2);
demos2.appendChild(link2);
sendlink(value2);
}
function Link3()
{
var demo3 = document.getElementById('demo3');
var demos3 = document.getElementById('demos3');
var value3 = prompt("Please Enter the Link");
var link3 = document.createElement('a');
link3.setAttribute('href', value3);
link3.innerHTML = "New Link3";
demo3.parentNode.removeChild(demo3);
demos3.appendChild(link3);
sendlink(value3);
}
function sendlink(str)
{
if (str.length==0)
{
document.getElementById("txtHint").innerHTML="hello";
return;
}
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","links.php?q="+str,true);
xmlhttp.send();
}
</script>
</body>
</html>
下面是$ C $下links.php接收的值(即链接)的test.php的会通过Ajax调用:
Below is the code for links.php which receives the value(i.e, link) the test.php sends through ajax call:
<?php
include 'test.php';
$p=$_REQUEST['q'];
?>
我能够写使用json_en code JSON文件。现在,我将不得不读取.json文件的链接,其关联到相应的变量,并保存回。我怎么会去吗?
I am able to write to json file using json_encode. Now I would have to read the link from .json file, associate it to the corresponding variable and save it back. How would I go about this?
推荐答案
要写入JSON:
file_put_contents('filename.json', json_encode($p));
要读取JSON:
$p = json_decode(file_get_contents('filename.json'));
显然,这确实没有错误检查可言,虽然。
Obviously this does no error checking at all though.
这篇关于将数据写入到从PHP文件的JSON文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!