Writing data to json file from PHP file

I have a test.php page that displays three buttons with three buttons "Add Link", when I click the buttons, the user sees a pop-up window. In the window, he adds a link. After adding a link, the base page will change from β€œAdd Link” to a hyperlink with a new link. Now I have to pass the new link that I get from the user from test.php to links.php using the ajax call. Links.php must have JSON code to write a link to another first.json file. first.jason will have a pair of key values ​​for the variable and the link. I will need to later extract the value from the .json file and restore it to an array, update the corresponding variable and save it back.

I still managed to get a new link from test.php and send it via an ajax call to links.php. I can also display the link I get and checked it out. Now I would like to copy the link into a .json file as a key pair. I am new to json and can't figure out how to do this. My $ p variable in link.php has a link.

Any pointers to the same will be helpful. Thank you

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> 

Below is the code for links.php, which gets the value (i.e. link), test.php sends via ajax call:

 <?php include 'test.php'; $p=$_REQUEST['q']; ?> 

I can write to json file using json_encode. Now I will need to read the link from the .json file, associate it with the corresponding variable and save it back. How can i do this?

+6
source share
4 answers

To write json:

 file_put_contents('filename.json', json_encode($p)); 

To read json:

 $p = json_decode(file_get_contents('filename.json')); 

Obviously, error checking is not performed at all.

+25
source

if you want to insert data into json file below one, it is useful

 function exportToJson() { mysql_connect("localhost", "root", ""); mysql_select_db("krasimir_benchmark"); $res = mysql_query("SELECT * FROM users ORDER BY id"); $records = array(); while($obj = mysql_fetch_object($res)) { $records []= $obj; } file_put_contents("data.json", json_encode($records)); } 
+2
source

If you have already created your json, you can simply do this:

 fwrite("yourjson.json",json_encode($yourvariablewithdata)) 

If you have NOT created your json file, you can create it using the fopen function. More details http://www.php.net/manual/es/function.fopen.php

+1
source

You can always JSON.stringify JSON on the client side using jQuery, for example:

 var dataForJson = JSON.stringify(your_aray); 

Then on the server side write it as is (no json_encode needed):

 $jsondata = $_POST['dataForJson']; $writeJson = file_put_contents('data_en.json', $jsondata); 
0
source

Source: https://habr.com/ru/post/949342/


All Articles