Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> problem przy kreowaniu xls - php/jquery
johny97
post 11.07.2018, 07:50:15
Post #1





Grupa: Zarejestrowani
Postów: 5
Pomógł: 0
Dołączył: 9.01.2018

Ostrzeżenie: (0%)
-----


Witam

Za pomocą poniższego kodu chciałem zapisać do pliku xls wybrane pozycje z wyniku zapytania do bazy (poprzez checkboxy), niestety po zainicjowaniu akcji nie kreuje się xls ...
widzę tylko w 'developer tools' chrome ze dane przez 'post' są przekazywane do porządanej podstrony (export.php) ale nic pozatym się nie dzieje.
Prosze o jakieś wskazówki gdzie może być błąd.

ponizej kod:

baza.php:

  1. <!DOCTYPE html>
  2. <html lang="pl">
  3. <head>
  4. ...
  5.  
  6.  
  7.  
  8. <script type="text/javascript">
  9.  
  10.  
  11.  
  12. $(function(){
  13. $("#myTable").tablesorter();
  14. });
  15.  
  16.  
  17. </script>
  18. <script type="text/javascript">
  19. $(function(){
  20. $("#myTable").tablesorter({ sortList: [[0,0], [1,0]] });
  21. });
  22.  
  23.  
  24.  
  25. </script>
  26.  
  27. </head>
  28.  
  29.  
  30. <body style="margin: 0; padding: 0">
  31. <div class="wrapper">
  32. <div class="baza">
  33.  
  34. <div id="myDiv"><h2><a href="baza_site.php" style="text-decoration: none;"># Baza Site</a></h2></div>
  35. <button class="btn btn-danger" id="export">export xls</button>
  36. <h6><i>ostatnia aktualizacja bazy - 19.06.2018</i></h6>
  37.  
  38.  
  39. </div>
  40.  
  41. <div class="search">
  42. <form action="baza_site.php" method="POST">
  43. <div class="input-group">
  44. <input type="text" class="form-control" placeholder="Search" name="search">
  45. <div class="input-group-btn">
  46. <button class="btn btn-default" type="submit" name="submit" >
  47. <i class="glyphicon glyphicon-search"></i>
  48. </button>
  49. </div>
  50. </div>
  51. </form>
  52. </div>
  53.  
  54. <div class="tabela">
  55.  
  56.  
  57.  
  58. // Include config file
  59. require_once 'config.php';
  60.  
  61. if (isset($_POST["submit"])) {
  62. $search = mysqli_real_escape_string($mysqli, trim($_POST['search']));
  63. mysqli_set_charset( $mysqli, 'utf8');
  64.  
  65. $sql = "SELECT * FROM baza_site WHERE site LIKE '%$search%' OR city LIKE '%$search%' OR address LIKE '%$search%' OR accessDetails LIKE '%$search%' OR ServiceArea LIKE '%$search%' OR locationType LIKE '%$search%'";
  66. if($result = $mysqli->query($sql)){
  67. if($result->num_rows > 0){
  68. $ilosc = mysqli_num_rows($result);
  69. echo "<b>ilość rekordów : " .$ilosc."</b>";
  70. echo "<table id='myTable' class='tablesorter-blackice'>";
  71. echo "<thead>";
  72. echo "<tr>";
  73. echo "<th style='text-align:center;' class='filter-false'><input type='checkbox' id='checkall'/></th>";
  74. echo "<th>site</th>";
  75. echo "<th>location type</th>";
  76. echo "<th>city</th>";
  77. echo "<th>address</th>";
  78. echo "<th>access details</th>";
  79. echo "<th>service area</th>";
  80. echo "</tr>";
  81. echo "</thead>";
  82. echo "<tbody>";
  83. while($row = $result->fetch_array()){
  84. echo "<tr>";
  85. echo "<td class='text-center'>"."<input type='checkbox' name='checkboxlist' class='checkitem' value=". $row['id']."/> </td>";
  86. echo "<td>" . $row['site']. "</td>";
  87. echo "<td>" . $row['locationType']. "</td>";
  88. echo "<td>" . $row['city'] . "</td>";
  89. echo "<td>" . $row['address'] . "</td>";
  90. echo "<td>" . $row['accessDetails'] . "</td>";
  91. echo "<td>" . $row['ServiceArea'] . "</td>";
  92.  
  93.  
  94. echo "</tr>";
  95. }
  96. echo "</tbody>";
  97. echo "</table>";
  98. // Free result set
  99. $result->free();
  100. } else{
  101. echo "<p class='lead'><em>Brak informacji w bazie.</em></p>";
  102. }
  103. } else{
  104. echo "ERROR: Could not able to execute $sql. " . $mysqli->error;
  105. }
  106. }
  107. // Close connection
  108. $mysqli->close();
  109. ?>
  110.  
  111.  
  112.  
  113. </div>
  114. </div>
  115. <script type="text/javascript">
  116.  
  117. $("#myTable").tablesorter({
  118. headers: {
  119. 0: { sorter: false, parser: false }
  120. }
  121. });
  122.  
  123.  
  124.  
  125. <script type="text/javascript">
  126.  
  127. $('#checkall').change(function(){
  128. $('.checkitem').prop("checked", $(this).prop("checked"))
  129. })
  130.  
  131. $('#export').click(function(){
  132. var id = $('.checkitem:checked').map(function(){
  133. return $(this).val()
  134. }).get().join(' ')
  135. $.post('export.php?p=xls', {id : id})
  136.  
  137. });
  138.  
  139.  
  140.  
  141. </body>
  142. </html>


export.php:

  1. <?php
  2.  
  3. require_once 'config.php';
  4. $id='';
  5. $output = '';
  6. $page = isset($_GET['p'])? $_GET['p'] : '';
  7. if($page == 'xls'){
  8. $myid = $_POST['id'];
  9. $id = str_replace('/', '', $myid);
  10.  
  11.  
  12. $sql = "SELECT * FROM baza_site WHERE id IN($id)";
  13. echo $sql;
  14. if($result = $mysqli->query($sql)){
  15. if($result->num_rows > 0)
  16. {
  17.  
  18. $output .= '
  19. <table border="1">
  20. <tr>
  21. <th>id</th>
  22. <th>site</th>
  23. <th>location type</th>
  24. <th>city</th>
  25. <th>address</th>
  26. <th>access details</th>
  27. <th>service area</th>
  28. </tr>
  29. ';
  30. while($row = mysqli_fetch_array($result))
  31. {
  32. $output .= '
  33. <tr>
  34. <td>'.$row["id"].'</td>
  35. <td>'.$row["site"].'</td>
  36. <td>'.$row["locationType"].'</td>
  37. <td>'.$row["city"].'</td>
  38. <td>'.$row["address"].'</td>
  39. <td>'.$row["accessDetails"].'</td>
  40. <td>'.$row["ServiceArea"].'</td>
  41.  
  42. </tr>
  43. ';
  44. }
  45. $output .= '</table>';
  46.  
  47.  
  48. header("Content-Type: application/vnd.ms-excel");
  49. header("Content-Disposition: attachment; filename=".'xxx'.date("Y-m-d_H_i_s").".xls");
  50. header("Pragma: no-cache");
  51. header("Expires: 0");
  52. echo $output;
  53. }
  54. }}
  55. $mysqli->close();
  56.  
  57.  
  58.  
  59.  
  60. ?>
Go to the top of the page
+Quote Post
Pyton_000
post 11.07.2018, 08:25:07
Post #2





Grupa: Zarejestrowani
Postów: 8 068
Pomógł: 1414
Dołączył: 26.10.2005

Ostrzeżenie: (0%)
-----


Wszystko zapewne działa tak jak powinno. Ale Ajax nie słuzy do pobierania plików generalnie...

https://nehalist.io/downloading-files-from-post-requests/
Go to the top of the page
+Quote Post
johny97
post 11.07.2018, 10:42:35
Post #3





Grupa: Zarejestrowani
Postów: 5
Pomógł: 0
Dołączył: 9.01.2018

Ostrzeżenie: (0%)
-----


Zatem jak najprościej przekazać wartośc 'id'(z zaznaczonych checkboxow) do podstrony która wykona zapytanie do bazy i wykreuje mi xls'a ?
Go to the top of the page
+Quote Post
nospor
post 11.07.2018, 11:33:45
Post #4





Grupa: Moderatorzy
Postów: 36 440
Pomógł: 6290
Dołączył: 27.12.2004




Normalnie, wyslij formularz do tej podstrony


--------------------

"Myśl, myśl, myśl..." - Kubuś Puchatek || "Manual, manual, manual..." - Kubuś Programista
"Szukaj, szukaj, szukaj..." - Kubuś Odkrywca || "Debuguj, debuguj, debuguj..." - Kubuś Developer

Go to the top of the page
+Quote Post
Pyton_000
post 11.07.2018, 11:56:59
Post #5





Grupa: Zarejestrowani
Postów: 8 068
Pomógł: 1414
Dołączył: 26.10.2005

Ostrzeżenie: (0%)
-----


Widać że nie przeczytałeś tego co podesłałem bo tam masz gotowe rozwiązanie tak na prawdę.
Go to the top of the page
+Quote Post
johny97
post 11.07.2018, 12:20:10
Post #6





Grupa: Zarejestrowani
Postów: 5
Pomógł: 0
Dołączył: 9.01.2018

Ostrzeżenie: (0%)
-----


jak widać dużo jeszcze się muszę nauczyć
ale rozwiązania problemu tam nie widze ;/
dzięki za pomoc
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Wersja Lo-Fi Aktualny czas: 28.03.2024 - 19:00