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;
?>