【问题标题】:How to show data in table received from server如何显示从服务器接收到的表中的数据
【发布时间】:2020-02-27 23:44:35
【问题描述】:

我有一个页面 index.html(带有表格和表格)和一个 php 文件名 indexx.php 当我在 index.html 中填写数据时,我单击按钮获取数据并请求转到我的文件 indexx.php。现在 indexx.php 从 HTML 页面获取数据并使用 post 方法请求一些视频,我成功接收 Json 数据。但我想以表格格式在我的 HTML 页面中显示这些数据。

index.html

<head>
</head>
<body>




<form action="indexx.php" method="POST" >
<div class="container" style="width:100%;">
<center></center>
</div>
<div class="container" style="width:100%;">
<label for="userId"><b>UserId</b></label>
<input type="number" placeholder="Enter Your User Id" name="userId" autofocus required>

<label for="categoryId"><b>categoryId</b></label>
<input type="number" placeholder="Enter categoryId" name="categoryId" required>

<button type="submit" >GET DATA</button>
</div>

</form>
<div class="wrapper">
<div class="profile">
  <table id= "userdata" width="50%" border="2">
    <thead>
      <th>VIDEO NAME</th>
      <th>DATE</th>
      <th>TIME</th>           
      <th>DURACTION</th>
      <th>LIVE STATUS</th>
      <th>LINK</th>
    </thead>
  <tbody>
    <!-- here you will loop through the data you have received from curl and fill it in the tbody, and this entrie thing will be done when the page loads -->
  </tbody>
</table>

</div>
</div>
</div>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"> 
</script>
</body>
</html>

我的 indexx.php


<?php
$userId = $_POST['userId'];
$categoryId = $_POST['categoryId'];
$url = 'https://www.serversample.php';
$data = array('userId' => $userId, 'categoryId' => $categoryId);

// use key 'http' even if you send the request to https://...
$options = array(
    'http' => array(
        'header'  => "Content-type: application/x-www-form-urlencoded\r\n",
        'method'  => 'POST',
        'content' => http_build_query($data)
    )
);
$context  = stream_context_create($options);
$result = file_get_contents($url, false, $context);
if ($result === FALSE) { /* Handle error */ }

print($result);

?>

我从另一台服务器收到的 JSON 文件示例

{"yourvideo":[{"Id":"55","videoName":"CLASS-3","date":"2 AUG 2015","time":"9:58 PM","video":"sCLKbhghghhgb","image":"https://img.youtube.com/vi/sjjZB4jjuhwc/0.jpg","videoType":"video","videoDuration":"02:08:88","liveStatus":"offline","testId":"0"},{"Id":"6985","videoName":"CLASS-4","date":"29 AUG 2018","time":"8:52 PM","video":"gtghnhyv","image":"https://img.youtube.com/vi/uygbnhjhg/0.jpg","videoType":"video","videoDuration":"02:81:18","liveStatus":"offline","testId":"0"}]}

现在我想在我的 index.html 页面中以表格格式显示这个文件

【问题讨论】:

    标签: javascript php jquery html html-table


    【解决方案1】:

    你可以使用

    json_encode($result, true);
    

    它将返回关联数组与数据。使用 print_r 打印并在没有第二个参数的情况下检查您将拥有带键的对象。

    【讨论】:

    • 先生,您可以编辑我的代码并粘贴到这里吗。因为我是编程新手
    • 只需在文件末尾写print_r(json_encode($result,true));,您将在输出中获得数组。将结果粘贴到此位置以进行下一步。
    • 在哪里(我的意思是在哪个文件中)我必须使用`json_encode($result, true);`
    • 就在indexx.php
    • 我试过了,但没有任何反应,只需打开 indexx.php Json 数据
    【解决方案2】:

    首先,不要使用 .html 文件,使用 .php 代替:index.php(你当前的 index.html 文件) 不需要处理 php 文件(您使用的是 indexx.php),因为一切都可以在 index.php 中完成,这是一个简单的示例:

    <form method='POST'>
        <input type='hidden' name='formSubmitted' value=1>
    
        <label for="userId"><b>UserId</b></label>
        <input type="number" placeholder="Enter Your User Id" name="userId" autofocus required>
    
        <label for="categoryId"><b>categoryId</b></label>
        <input type="number" placeholder="Enter categoryId" name="categoryId" required>
    
        <button type="submit" >GET DATA</button>
    </form>
    
    <!-- form processing STARTS here -->
    
    <?php
    
    if (isset($_POST['formSubmitted'])) {
        print_r($_POST); // you know what to do with this
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-06-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多