douxin2003 2017-03-20 08:41
浏览 293

将json文件加载到变量中

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.
"."</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
 "."</div> <br>";
        echo "<div style='font-size: 150%;'>". "Returning to Home...
 "."</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.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
    • ¥20 关于URL获取的参数,无法执行二选一查询
    • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
    • ¥15 marlin编译错误,如何解决?
    • ¥15 有偿四位数,节约算法和扫描算法
    • ¥15 VUE项目怎么运行,系统打不开
    • ¥50 pointpillars等目标检测算法怎么融合注意力机制
    • ¥20 Vs code Mac系统 PHP Debug调试环境配置
    • ¥60 大一项目课,微信小程序
    • ¥15 求视频摘要youtube和ovp数据集