Как показать данные в таблице, полученные с сервера - PullRequest
0 голосов
/ 01 ноября 2019

У меня есть страница 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

Ответы [ 2 ]

0 голосов
/ 01 ноября 2019

Прежде всего, не используйте файлы .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
}
0 голосов
/ 01 ноября 2019

Вы можете использовать

json_encode($result, true);

Он вернет ассоциативный массив с данными. Напечатайте с print_r и проверьте без второго аргумента у вас будет объект с ключами.

...