如何在动态选择框上显示动态值

u3r8eeie  于 2021-06-15  发布在  Mysql
关注(0)|答案(1)|浏览(304)

我在找人帮忙
我在bootstrap中使用了一个添加多行的代码,在这个代码中,我试图从选择框中获取值,在javascript中使用php,使用ajax,它从数据库中获取值,对于第一行来说效果很好,但是当涉及到多行时,它只获取值,然后再次添加到第一行,我需要的是在同一行上获取值,从这里选取的是代码

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <!-- Required meta tags-->
  5. <meta charset="UTF-8">
  6. <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  7. <meta name="description" content="au theme template">
  8. <meta name="author" content="Hau Nguyen">
  9. <meta name="keywords" content="au theme template">
  10. <!-- Title Page-->
  11. <title>Dashboard 3</title>
  12. <!-- Fontfaces CSS-->
  13. <link href="css/font-face.css" rel="stylesheet" media="all">
  14. <link href="vendor/font-awesome-4.7/css/font-awesome.min.css" rel="stylesheet" media="all">
  15. <link href="vendor/font-awesome-5/css/fontawesome-all.min.css" rel="stylesheet" media="all">
  16. <link href="vendor/mdi-font/css/material-design-iconic-font.min.css" rel="stylesheet" media="all">
  17. <script src="//code.jquery.com/jquery-1.11.1.min.js"></script>
  18. <!-- Bootstrap CSS-->
  19. <link href="vendor/bootstrap-4.1/bootstrap.min.css" rel="stylesheet" media="all">
  20. <!-- Vendor CSS-->
  21. <link href="vendor/animsition/animsition.min.css" rel="stylesheet" media="all">
  22. <link href="vendor/bootstrap-progressbar/bootstrap-progressbar-3.3.4.min.css" rel="stylesheet" media="all">
  23. <link href="vendor/wow/animate.css" rel="stylesheet" media="all">
  24. <link href="vendor/css-hamburgers/hamburgers.min.css" rel="stylesheet" media="all">
  25. <link href="vendor/slick/slick.css" rel="stylesheet" media="all">
  26. <link href="vendor/select2/select2.min.css" rel="stylesheet" media="all">
  27. <link href="vendor/perfect-scrollbar/perfect-scrollbar.css" rel="stylesheet" media="all">
  28. <!-- Main CSS-->
  29. <link href="css/theme.css" rel="stylesheet" media="all">
  30. </head>
  31. <?php
  32. $connection = mysqli_connect('localhost','root','','quotation');
  33. ?>
  34. <?php
  35. if(isset($_POST['submit']))
  36. {
  37. $name = $_POST['name'];
  38. $mail = $_POST['mail'];
  39. $phone = $_POST['phone'];
  40. $name1 = count($_POST['name']);
  41. $mail1 = count($_POST['mail']);
  42. $phone1 = count($_POST['phone']);
  43. print_r($name);
  44. echo "<br>";
  45. print_r($mail);
  46. echo "<br>";
  47. print_r($phone);
  48. echo "<br>";
  49. echo $name1;
  50. echo $mail1;
  51. echo $phone1;
  52. }
  53. ?>
  54. <br>
  55. <div class="container">
  56. <div class="row">
  57. <div class="col col-sm6">
  58. <a href="#">
  59. <img src="images/icon/Omni_logo_for_web2.png" alt="CoolAdmin" />
  60. <p style="color:gray;"><i>Transforming People and Business</i></p>
  61. </a>
  62. <p>
  63. A-242, Sardar Ali Sabri Rd. <br>
  64. Block-2, Gulshan-e-Iqbal <br>
  65. Karachi. <br>
  66. Phone: 021-3498OMNI(6664) Mobile: 0312-2169325, 0337-7222191
  67. <br> SNTN : S0529023-6
  68. <br>
  69. TaxpayerName : OMNI ACADEMY
  70. </p>
  71. </div>
  72. <div class="col col-sm6">
  73. <h2 style="color:gray">Quotations</h2>
  74. <p>
  75. DATE 2018-12-17
  76. <br>
  77. RFQ# 0308-2018
  78. <br>
  79. Karachi. <br>
  80. Phone: 021-3498OMNI(6664) Mobile: 0312-2169325, 0337-7222191
  81. <br> SNTN : S0529023-6
  82. Customer ID 408
  83. <br>
  84. Customer NTN/SNTN NA
  85. <br>
  86. <br>
  87. Customer Lakson Group
  88. <br>
  89. Valid until: 28-Nov-2018
  90. <br>
  91. Prepared by: FIN-03
  92. </p>
  93. </div>
  94. </div>
  95. </div>
  96. <br>
  97. <form method="post">
  98. <div class="container">
  99. <table id="myTable" class=" table order-list">
  100. <thead>
  101. <tr>
  102. <td>Name</td>
  103. <td>Gmail</td>
  104. <td>Phone</td>
  105. </tr>
  106. </thead>
  107. <tbody>
  108. </tbody>
  109. <tfoot>
  110. <tr>
  111. <td colspan="5" style="text-align: left;">
  112. <input type="button" class="btn btn-lg btn-block " id="addrow" value="Add Row" />
  113. </td>
  114. </tr>
  115. <tr>
  116. </tr>
  117. </tfoot>
  118. </table>
  119. </div>
  120. <input type="submit" name = 'submit' value="Subm">
  121. </form>
  122. <script>
  123. $(document).ready(function () {
  124. var counter = 0;
  125. $("#addrow").on("click", function () {
  126. var newRow = $("<tr>");
  127. var cols = "";
  128. cols += '<td><select onchange = "myfunc(this.value)" name="name[]" class="form-control" ' + counter + '" ><?php $select_courses = "select * from courses";
  129. $run_select = mysqli_query($connection,$select_courses);
  130. while($row = mysqli_fetch_assoc($run_select)){
  131. $name = $row["name"];
  132. $course_id = $row["course_id"];
  133. ?><option value="<?php echo $course_id ?>"><?php echo $name; ?></option> <?php } ?></select></td>';
  134. cols += '<td id="getdata" ' + counter + '"></td>';
  135. cols += '<td><input type="text" class="form-control" name="phone[]' + counter + '"/></td>';
  136. cols += '<td><input type="button" class="ibtnDel btn btn-md btn-danger " value="Delete"></td>';
  137. newRow.append(cols);
  138. $("table.order-list").append(newRow);
  139. counter++;
  140. });
  141. $("table.order-list").on("click", ".ibtnDel", function (event) {
  142. $(this).closest("tr").remove();
  143. counter -= 1
  144. });
  145. });
  146. function calculateRow(row) {
  147. var price = +row.find('input[name^="price"]').val();
  148. }
  149. function calculateGrandTotal() {
  150. var grandTotal = 0;
  151. $("table.order-list").find('input[name^="price"]').each(function () {
  152. grandTotal += +$(this).val();
  153. });
  154. $("#grandtotal").text(grandTotal.toFixed(2));
  155. }
  156. function myfunc(datavalue)
  157. {
  158. $.ajax({
  159. url:'getdata.php',
  160. type:'post',
  161. data:{
  162. datapost:datavalue
  163. },
  164. success:function(result){
  165. $('#getdata').html(result);
  166. }
  167. });
  168. }
  169. </script>
  170. <?php include('include/footer.php'); ?>
  171. <!-- end document-->
  172. getdata.php
  173. <?php
  174. $connection = mysqli_connect('localhost','root','','quotation');
  175. ?>
  176. <?php
  177. $name_id = $_POST['datapost'];
  178. $q = "select * from courses where course_id = '$name_id'";
  179. $result = mysqli_query($connection,$q);
  180. $rows = mysqli_fetch_assoc($result);
  181. ?>
  182. <input type="text" name = "phone[]" value="<?php echo $rows['name']; ?>">
5cnsuln7

5cnsuln71#

您可以执行以下操作:
将唯一id设置为 select 以及 getdata .

  1. <select id="select'+counter+'">
  2. <td id="getdata'+counter+'"></td>

您可以调用 select 单独或将id传递给 myfunc 从那里分析。

  1. $(document).on('change', 'select[name="name[]"]', function(){
  2. idName = $(this).attr('id'); //finding id of the element
  3. id = idName.substring(6, idName.length); //finding id number
  4. var datavalue = $(this).val();
  5. myfunc(datavalue, id);
  6. });

改变你的想法 myfunc 如下所示。

  1. function myfunc(datavalue, id) {
  2. $.ajax({
  3. url:'getdata.php',
  4. type:'post',
  5. data:{
  6. datapost:datavalue
  7. },
  8. success:function(result){
  9. $('#getdata'+id).html(result);
  10. }
  11. });
  12. }

此外,在删除行时,还需要小心维护唯一的id。您可以在这里查看如何动态添加和删除行。

展开查看全部

相关问题