0

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.

georges619
  • 288
  • 1
  • 6
  • 18
  • you want to change `default.json` into a variable file ? – hassan Mar 20 '17 at 09:04
  • Yes, I would like to do the same thing I did in the PHP example. – georges619 Mar 20 '17 at 09:07
  • how do you will get that variable ? how do you will send it's value to javascript – hassan Mar 20 '17 at 09:08
  • That's why I asked this question. I wonder if this is possible in javascript. If this is not possible, the only solution is to modify the file which I want to load ? – georges619 Mar 20 '17 at 09:11
  • i mean you need to read this file from user ? or uploaded file ? or from text input ? – hassan Mar 20 '17 at 10:42
  • Once the file is uploaded, I want to read its content – georges619 Mar 20 '17 at 10:44
  • you will need to upload your file using ajax completely , here is a good example , http://stackoverflow.com/a/23981045/2359679 , you've prepared your php upload code, so all you will need is to connect between your php and your ajax code – hassan Mar 20 '17 at 10:54
  • My web server is running on Linux, and PHP is not supported :/ – georges619 Mar 20 '17 at 11:01
  • linux supports php! simply install it – hassan Mar 20 '17 at 11:02
  • Yes I know, but the web server I use don't support it (it is Libwebsockets). My server is on a microcomputer (embedded Linux card). I thank you for your replies. – georges619 Mar 20 '17 at 12:33

0 Answers0