I have created a PHP programm which allows to load a random .json file on my computer. When I select the file I want to display, I get the data from the selected .json file. At the beginning, I used a default file which I replaced with a variable. Here is the PHP code :
<?php
$uploaddir = 'uploads/';
$uploadfile = $uploaddir . basename($_FILES['file']['name']);
$file_test = explode('.', $_FILES['file']['name']);
$file_ext= strtolower(end($file_test));
$expensions = array(".json");
if(in_array($file_ext,$expensions)=== false){
$errors[]="extension not allowed, please choose a DBC or an XML file.";
}
if (move_uploaded_file($_FILES['file']['tmp_name'], $uploadfile)) {
echo "<div style='font-size: 150%;'>". "File is valid, and was successfully uploaded.\n"."</div>";
} else {
echo "<div style='font-size: 150%;'>". "Oops, It seems you haven't uploaded a file ! Please make sure you have selected a .json file\n "."</div> <br>";
echo "<div style='font-size: 150%;'>". "Returning to Home...\n "."</div>";
header("Refresh: 3,URL=upload_file.php");
exit();
}
echo basename($_FILES['file']['name']);
?>
<?php
header("Refresh: 2,URL=json_content_extended.php?filename=".$uploadfile);
exit();
?>
This file contains the code where I declared the requested value :
<?php
$json_file = file_get_contents($_REQUEST["filename"]);
$jfo = json_decode($json_file, true);
?>
And here is the HTML part :
<!DOCTYPE html>
<html>
<head>
<title>Menu</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<form action="upload.php" method="POST" enctype="multipart/form-data" >
<div class="test">
<input type="file" accept=".json" name="file"/>
<button class="up" name="click" class="click">Submit file</button>
</div>
</form>
</body>
</html>
This is just an example ! What I actually want to do is the same thing in Javascript. I started the script with a default file, but now I would like to replace this file (default.json) with a variable. I use $.getJson. Here is the code :
$(document).ready(function() {
var files = ["default.json"];
$.getJSON(files, function(json) {
var tr0, tr1, tr2, tr3, tr4, tr5, tr6, tr7, tr8, tr9;
tr0 = $('<tr/>');
tr0.append("<td>" + '<input class="expand" name="bt0" type="button" value="+" />' + "</td>");
tr0.append("<td>" + json.messages[0].id + "</td>");
tr0.append("<td>" + json.messages[0].name + "</td>");
tr0.append("<td>" + json.messages[0].is_extended_frame + "</td>");
I wonder if there is somehow a possibility to set a variable instead of the file name. I would like to do the same thing I did in the PHP example, but this time in javascript.