Guest User

Untitled

a guest
Jul 15th, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.42 KB | None | 0 0
  1. <div class="container">
  2. <div class="row" style="margin-top: 1rem;">
  3. <div class="col-sm">
  4. <form action="" method="post">
  5. <table class="fit" id="entry">
  6. <tr>
  7. <td class="fit"><label for="start">Planet (try <strong>Caprica</strong> or <strong>Picon</strong>): </label></td>
  8. </tr>
  9. <tr>
  10. <td class="fit"><input type="test" id="planet" name="planet" required autofocus /></td>
  11. </tr>
  12. </table>
  13. <input class="btn btn-primary" type="submit" value="Get Characters" />
  14. </form>
  15. </div>
  16. </div>
  17. </div>
  18.  
  19. <div class="container" style="margin-top: 2rem;">
  20. <div class="row">
  21. <div class="col-sm">
  22. <?php
  23. require_once('./resources/pdo.php');
  24.  
  25. if ( isset($_POST['planet']) ) {
  26.  
  27. $planet = strtolower($_POST['planet']);
  28. $pdo = new myPDO('phppostpost');
  29.  
  30. try {
  31. $stmt = $pdo->prepare('CALL devCharacters(?)');
  32. $stmt->bindParam(1, $planet, PDO::PARAM_STR);
  33. $stmt->execute();
  34. $stmt->setFetchMode(PDO::FETCH_ASSOC);
  35. } catch (PDOException $e) {
  36. die("Error occurred: " . $e->getMessage());
  37. }
  38. ?>
  39.  
  40. <div class="table-responsive">
  41.  
  42. <table class="table table-striped table-hover">
  43. <thead class="thead-light">
  44. <tr>
  45. <th class="fit">Select</th>
  46. <th class="fit" scope="col">Customer First</th>
  47. <th class="fit" scope="col">Customer Last</th>
  48. <th class="fit" scope="col">Planet</th>
  49. </tr>
  50. </thead>
  51. <tbody>
  52. <?php while ($r = $stmt->fetch()): ?>
  53. <tr>
  54. <?php echo "<td class='fit'><input type='radio' id='cust-" . $r['customer_id'] ."' name='cust-id' value='". $r['customer_id'] . "' </td>"; ?>
  55. <?php echo "<td class='fit'>" . $r['first_name'] . "</td>"; ?>
  56. <?php echo "<td class='fit'>" . $r['last_name'] . "</td>"; ?>
  57. <?php echo "<td class='fit'>" . $r['origin_planet'] . "</td>"; ?>
  58. </tr>
  59. <?php endwhile; ?>
  60. </tbody>
  61. </table>
  62. </div>
  63. <input class="btn btn-primary" onclick="getSelectedRowData();" type="submit" value="Send" />
  64. <?php } ?>
  65. </div>
  66. </div>
  67. </div>
  68.  
  69. <?php echo "<td class='fit' id='fname-" . $r['customer_id'] ."'>" . $r['first_name'] . "</td>"; ?>
  70. <?php echo "<td class='fit' id='lname-" . $r['customer_id'] ."'>" . $r['last_name'] . "</td>"; ?>
  71. <?php echo "<td class='fit' id='planet-" . $r['customer_id'] ."'>" . $r['origin_planet'] . "</td>"; ?>
  72.  
  73. <tbody id="results-body">
  74.  
  75. function getSelectedRowData() {
  76. const tableRowArray = Array.from([document.getElementById('results-body')][0].rows);
  77.  
  78. let custFirst;
  79. let custLast;
  80. let custPlanet;
  81.  
  82. tableRowArray.forEach((tableRow, i) => {
  83. cellButton = tableRow.getElementsByTagName('input');
  84. if (cellButton[0].checked == true ) {
  85. const rowID = cellButton[0].id.split('-').pop();
  86. custFirst = document.getElementById('fname-' + rowID).innerHTML;
  87. custLast = document.getElementById('lname-' + rowID).innerHTML;
  88. custPlanet = document.getElementById('planet-' + rowID).innerHTML;
  89. }
  90. });
  91.  
  92. /* Build a hidden form solution to prep for post.
  93. Source: https://stackoverflow.com/questions/26133808/javascript-post-to-php-page */
  94. let hiddenForm = document.createElement('form');
  95.  
  96. hiddenForm.setAttribute('method', 'post');
  97. hiddenForm.setAttribute('action', 'newpage.php');
  98. hiddenForm.setAttribute('target', 'view');
  99.  
  100. const fieldCustFirst = document.createElement('input');
  101. const fieldCustLast = document.createElement('input');
  102. const fieldCustPlanet = document.createElement('input');
  103.  
  104. fieldCustFirst.setAttribute('type', 'hidden');
  105. fieldCustFirst.setAttribute('name', 'custFirst');
  106. fieldCustFirst.setAttribute('value', custFirst);
  107.  
  108. fieldCustLast.setAttribute('type', 'hidden');
  109. fieldCustLast.setAttribute('name', 'custLast');
  110. fieldCustLast.setAttribute('value', custLast);
  111.  
  112. fieldCustPlanet.setAttribute('type', 'hidden');
  113. fieldCustPlanet.setAttribute('name', 'custPlanet');
  114. fieldCustPlanet.setAttribute('value', custPlanet);
  115.  
  116. hiddenForm.appendChild(fieldCustFirst);
  117. hiddenForm.appendChild(fieldCustLast);
  118. hiddenForm.appendChild(fieldCustPlanet);
  119.  
  120. document.body.appendChild(hiddenForm);
  121.  
  122. // Post
  123. window.open('', 'view');
  124. hiddenForm.submit();
  125. }
Add Comment
Please, Sign In to add comment