By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,264 Members | 1,228 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,264 IT Pros & Developers. It's quick & easy.

how can I fetch dynamic rows (drop down's database values) using ajax?

P: 9
My javaScript.
Expand|Select|Wrap|Line Numbers
  1. <script language="javascript" type="text/javascript">
  2. var i=0;
  3. function addRow(table1)
  4. {
  5. i++;
  6. frm.reza.value=i;
  7.  
  8. var tbl = document.getElementById('table1');
  9. var lastRow = tbl.rows.length;
  10. var iteration = lastRow -1;
  11. var row = tbl.insertRow(lastRow);
  12.  
  13. var firstCell = row.insertCell(0);
  14. var el = document.createElement('input');
  15. el.type = 'text';
  16. el.name = 'title' + i;
  17. el.id = 'title' + i;
  18. el.size = 20;
  19. el.maxlength = 20;
  20. firstCell.appendChild(el);
  21.  
  22. var secondCell = row.insertCell(1);
  23. var el2 = document.createElement('input');
  24. el2.type = 'time';
  25. el2.name = 'start_time' + i;
  26. el2.id = 'start_time' + i;
  27. el2.size = 20;
  28. el2.maxlength = 20;
  29. secondCell.appendChild(el2);
  30.  
  31. var thirdCell = row.insertCell(2);
  32. var el3 = document.createElement('input');
  33. el3.type = 'time';
  34. el3.name = 'end_time' + i;
  35. el3.id = 'end_time' + i;
  36. el3.size = 20;
  37. el3.maxlength = 20;
  38. thirdCell.appendChild(el3);
  39.  
  40. var forthCell = row.insertCell(3);
  41. var el4 = document.createElement("select");
  42. el4.name ='program_name'+i;
  43. el4.id = 'program_name' + i;
  44. var option = document.createElement("option");
  45. option.value='';
  46. option.innerHTML='';
  47. el4.appendChild(option);
  48. forthCell.appendChild(el4);
  49.  
  50. var fifthCell = row.insertCell(4);
  51. var el5 = document.createElement("select");
  52. el5.name ='peak_offpeak'+i;
  53. var option1 = document.createElement("option");
  54. option1.value='';
  55. option1.innerHTML='';
  56. el5.appendChild(option1);
  57.  
  58. var option2 = document.createElement("option");
  59. option1.value='peak';
  60. option1.innerHTML='peak';
  61. el5.appendChild(option1);
  62.  
  63. var option3 = document.createElement("option");
  64. option2.value='off_peak';
  65. option2.innerHTML='off_peak';              
  66. el5.appendChild(option2);
  67. fifthCell.appendChild(el5);
  68.  
  69. var sixthCell = row.insertCell(5);
  70. var el6 = document.createElement("select");
  71. el6.name ='sub_brand_name'+i;
  72. el6.id = 'sub_brand_name' + i;
  73. var option = document.createElement("option");
  74. option.value='';
  75. option.innerHTML='txt';
  76. el6.appendChild(option);
  77. sixthCell.appendChild(el6);
  78.  
  79.  
  80.  
  81. var seventhCell = row.insertCell(6);
  82. var el7 = document.createElement('input');
  83. el7.type = 'text';
  84. el7.name = 'sponsor' + i;
  85. el7.id = 'sponsor' + i;
  86. el7.size = 20;
  87. el7.maxlength = 20;
  88. seventhCell.appendChild(el7);    
  89.  
  90. var eighthCell = row.insertCell(7);
  91. var el8 = document.createElement("select");
  92. el8.name ='product_name'+i;
  93. el8.id = 'product_name' + i;
  94. var option = document.createElement("option");
  95. option.value='';
  96. option.innerHTML='+i+';
  97. el8.appendChild(option);
  98. eighthCell.appendChild(el8);
  99.  
  100. }
  101. </script>
  102.  
  103.  
My HTML and PHP code.
Expand|Select|Wrap|Line Numbers
  1.  
  2. <head>
  3. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  4. <title>Untitled Document</title></head>
  5.  
  6. <body>
  7. <header><ul><tr><li><a href="https://bytes.com/submit/mediasourcebd_reza/home.php">Home</a></li></tr></ul></header>
  8. <form action="action_entry_history.php" method="post" name="frm" id="frm">
  9. <table width="100%" border="0"  id="table1" class="table">
  10. <tr align="justify">
  11. <td><strong>Title</strong></td>
  12. <td><strong>Start Time</strong> </td>
  13. <td><strong>End Time</strong> </td>
  14. <td><strong>Program</strong> </td>
  15. <td><strong>Peak Off-Peak</strong> </td>
  16. <td><strong>Sub-Brand</strong> </td>
  17. <td><strong>Sponsor</strong> </td>
  18. <td><strong>Product</strong> </td>
  19. </tr>
  20. <tr>
  21. <td><input name="title" type="text" id="title" size="20" maxlength="20" /></td>
  22. <td><input name="start_time" type="time" id="start_time" size="20" maxlength="20" /></td>
  23. <td><input name="end_time" type="time" id="end_time" size="20" maxlength="20" /></td>
  24. <td><?php 
  25.                         $sql ="SELECT program_name FROM program";
  26.                         $result = mysql_query($sql);
  27.                         echo "<select name='program_name' id='ss' class='s' >";
  28.                         while ($row = mysql_fetch_array($result)) {
  29.                         echo "<option value='" .$row['program_name'] ."'>" .$row['program_name'] ."</option>";
  30.                         }
  31.                         echo "</select>";        
  32.                         ?></td>
  33. <td><select name="peak_offpeak"><option value="peak">peak</option><option value="off_peak">off_peak</option></select></td>
  34. <td><?php 
  35.                         $sql = "SELECT sub_brand_name FROM sub_brand";
  36.                         $result = mysql_query($sql);
  37.                         echo "<select name='sub_brand_name' >";
  38.                         while ($row = mysql_fetch_array($result)) {
  39.                         echo "<option value='" . $row['sub_brand_name'] ."'>".$row['sub_brand_name']."</option>";
  40.                         }
  41.                         echo "</select>";        
  42.                         ?></td>
  43.  
  44. <td><input name="sponsor" type="text" id="sponsor" size="20" maxlength="20" /></td>
  45. <td><?php 
  46.                         $sql = "SELECT product_name FROM product";
  47.                         $result = mysql_query($sql);
  48.                         echo "<select name='product_name' >";
  49.                         while ($row = mysql_fetch_array($result)) {
  50.                         echo "<option value='".$row['product_name'] ."'>".$row['product_name']."</option>";
  51.                         }
  52.                         echo "</select>";        
  53.                         ?></td>
  54.  
  55. </tr>
  56.  
  57. </table>
  58. <input type="button" value="Add Rows" onclick="addRow();" />
  59. <input name="Save" type="submit" value="Save Rows" />
  60. <input type="hidden" name="reza" id="reza" value="0" />
  61. </form>
  62.  
May 17 '16 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.