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) {</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?
source share