2

Assume a JSON file on a server contains ['one','two','three'].

If an HTML button click event occurs and a variable is set to 'two',
How do you use HTTP request and PHP to delete 'two' from the JSON file if the num variable value matches 'two' in the JSON file?

UPDATE!!! Same value returns after button click

Files (JS & HTML):

$('#button1').click(function() {
  let num = 'two';
  $.ajax({
    type: "POST",
    url: "./update.php",
    data: num
  });
});
<html>

<head>
  <title>Button Writer</title>
</head>

<body>
  <button id="button1" type="button">Write to File</button>



  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

  <script src="./app.js"></script>
</body>

</html>

JSON File: (codes.json)

["one","two","three"]

PHP File (update.php)

<?php
$jsonContents = file_get_contents('./codes.json');//Read here the json file into text
$array = json_decode($jsonContents);

$value = $_POST;

if (($key = array_search($value, $array )) !== false) {
   unset($array [$key]);
}

$jsonString = json_encode($array);
//Here you can save the content of $jsonString into the file again
file_put_contents("./codes.json", $jsonString);

echo $jsonString;
?>
7
  • "if this value matches 2" ...what value are you talking about? and what do you want to do with the new altered JSON? put it back in the file? Commented Feb 9, 2018 at 20:57
  • @Iwrestledabearonce. what I mean is to send an HTTP request the value of the variable num to splice out 'two' from the JSON file if there is a match. Commented Feb 9, 2018 at 21:02
  • the value of what variable? Commented Feb 9, 2018 at 21:06
  • @Iwrestledabearonce. Take a look at the JS file. let num = 'two'; on button click event Commented Feb 9, 2018 at 21:09
  • secure.php.net/manual/en/function.array-splice.php Commented Feb 9, 2018 at 21:13

1 Answer 1

1

Then on the server, you can:

$jsonContents = file_get_contents('path/to/file.json'); //Read here the json file into text
$array = json_decode($jsonContents);

$value = $_POST["num"];

if (($key = array_search($value, $array )) !== false) {
   unset($array[$key]);
}

$jsonString = json_encode($array);
//Here you can save the content of $jsonString into the file again
file_put_contents('path/to/file.json', $jsonString);
Sign up to request clarification or add additional context in comments.

5 Comments

+1 if you change the first line to $jsonContents = file_get_contents('path/to/file.json'); // comment... so PHP doesn't throw a syntax error
@Tyblitz I changed the first line and tried to write to file file_put_contents("./codes.json", $jsonString); but values in the JSON file still returns the same. Check the contents of my PHP code above to see if I did something wrong
@claOnline The code is valid, except you should remove the space between $array [$key]. If it's not that, you probably have a server permission issue (check PHP function chmod), try doing chmod('./code.json', 0755) before you file_put_contents.
@Tyblitz. Did everything removed space and added chmod function. It still does not work. Still returns ["one","two","three"] instead of ["one","three"] Anyway, I am testing with XAMPP on windows 10 pc.
@claOnline Check all other possible points of failure. Does your AJAX call succeed, does PHP respond, etc.? In any case, this falls outside the scope of your original question, and you should probably signal to the community that this answer is correct by accepting it as it does answer the question in the title.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.