Как показать php ответили данные в html таблице - PullRequest
0 голосов
/ 02 ноября 2019

Я пытаюсь получить файл .json с другого сервера и хочу показать файл .json в виде таблицы html. PHP-код успешно возвращает JSON. Но моя проблема в том, что я хочу просмотреть это в таблице, потому что я не могу реализовать результат php в javascript, когда я нажму на кнопку с именем Get Data

Вот мой файл .php

<html>
<header>
    <title>Hello
    </title>
    </header>
<body>


<!--javascript make append table data-->

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

<script>

    $(function() {



   $.getJSON('$result.json', function(data) {
       $.each(data.videoLectureListing, function(i, f) {
          var tblRow = "<tr>" + "<td>" + f.Id + "</td>" +
           "<td>" + f.videoName + "</td>" + "<td>" + f.date + "</td>" + "<td>" + f.time + "</td>" + "<td>" + f.video + "</td>" + "<td>" + f.image + "</td>" +
           "<td>" + f.videoDuration + "</td>" + "<td>" + f.liveStatus + "</td>" + "<td>"+ "</tr>"
           $(tblRow).appendTo("#userdata tbody");
     });

   });

});
</script>




<!--javascript  append table data ended -->


<!-- user form -->
<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>
<!-- user form -->



<!--table-->

<div class="wrapper">
<div class="profile">
   <table id= "userdata" width="100%" border="5">
  <thead>
            <th>VIDEO NAME</th>
            <th>DATE</th>
            <th>TIME</th>
            <th>DURACTION</th>
            <th>LINK </th>
        </thead>
      <tbody>

       </tbody>
   </table>

</div>
</div>

<!--table data end-->



<!---php file start-->
<?php

$userId = $_POST['userId'];
$categoryId = $_POST['categoryId'];

if (isset($_POST['formSubmitted'])) {

$url = 'https://www.otherserver.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 */ }

echo ($result);
//json_encode($result, true);
}
?>


<!---php file ended    


</body>
</html>

1 Ответ

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

Пожалуйста, добавьте действие атрибут в тег формы.

<!-- user form -->
    <form method='POST' action='php_file_path'>
        <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>
    <!-- user form -->
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...