php - how to count number of rows in html table generated dynamically? -


can please me count number of rows in html table dynamically generated using javascript?if can in let me know have trying insert data of dynamically generated rows database

my js code :

<script language="javascript">  var count=1;     function addrow(tableid) {         /*if(empty(index))         {         var index = 0;          }         else         {         index++;         alert(index);         document.getelementbyid('cat').name = 'cat'+index;         }*/          var table = document.getelementbyid(tableid);          var rowcount = table.rows.length;         var count =  rowcount-1;         alert(rowcount);          var selectofcat = $("#"+tableid).find('tr:eq(0)').find('.cat').get(0);         var row = table.insertrow(rowcount);          var colcount = table.rows[0].cells.length;          for(var i=0; i<colcount; i++) {              var newcell = row.insertcell(i);              newcell.innerhtml = table.rows[0].cells[i].innerhtml;             //alert(newcell.childnodes);             switch(newcell.childnodes[0].type) {                 case "text":                         newcell.childnodes[0].value = "";                         break;                 case "checkbox":                         newcell.childnodes[0].checked = false;                         break;                 case "select-one":                         newcell.childnodes[0].selectedindex = 0;                         break;             }         }          $(table).find("tr:eq("+rowcount+")").find('.cat').attr("id","cat"+rowcount);      }      function deleterow(tableid) {         try {         var table = document.getelementbyid(tableid);         var rowcount = table.rows.length;          for(var i=0; i<rowcount; i++) {             var row = table.rows[i];             var chkbox = row.cells[0].childnodes[0];             if(null != chkbox && true == chkbox.checked) {                 if(rowcount <= 1) {                     alert("cannot delete rows.");                     break;                 }                 table.deleterow(i);                 rowcount--;                 i--;             }           }         }catch(e) {             alert(e);         }     }     function changeselection(value){    var length = document.getelementbyid("ind").options.length;    if(value == "all"){   for(var = 1;i<length;i++)     document.getelementbyid("ind").options[i].selected = "selected";    document.getelementbyid("ind").options[0].selected = "";   } 

}

</script> 

my php code:

                        <table width="100%">                             <tr>                                 <td><h5>add products</h5>&nbsp;&nbsp;&nbsp;                                 </td>                             </tr>                             <tr><td>                            <input id="add_row" type="button" value="add row" onclick="addrow('datatable')" class="btn btn-danger" name="add_row" />  <input type="button" value="delete row" onclick="deleterow('datatable')" class="btn btn-danger" /> <tr> <td bgcolor="red"> <table width="100%" border="1" cellpadding="0" cellspacing="2" >                              <tr><td width="2%">::</td><td style="width:213px;padding:5px;">type</td><td class="srcinfo_head">product</td><td  class="srcinfo_head">specification</td><td class="srcinfo_head_op">option</td><td class="srcinfo_head_src">source</td><td  class="srcinfo_head">frequency</td><td  class="srcinfo_head">quantity</td><td  class="srcinfo_head">unit</td>                             </tr> </table> </td> </tr> <tr><td> <table id="datatable" width="100%" border="1" cellpadding="0" cellspacing="2" style="border:#cccccc; border-width:3px; border-style:solid">      <tr valign="top">         <td width="2%"><input type="checkbox" name="chk" style="margin:5px;"/></td>         <td class="srcinfo">     <select  id="cat" class="cat" name="data[cat][]" size="10px" class="srcinfo_type" style ="height:77px !important;">     <option value="">--select category--     <?= $options ?> </select>          </td>          <td width="15%"><?php echo tep_draw_input_field('data[productname][]','','class="srcinfo"'); ?></td>         <td width="15%">         <?php echo tep_draw_textarea_field('data[specification][]', 'soft', 10,3, $http_post_vars['specification'],'class="srcinfo"'); ?>         </td>         <td width="10%"><div class="srcinfo_op">         <?php echo tep_draw_checkbox_field('data[option][]','purchase', false).'&nbsp;purchase';               echo tep_draw_checkbox_field('data[option][]','sell', false).'&nbsp;sell';            ?>         </div>          </td>         <td width="15%">          <?php   $source_array = array();                 $source_array[0] = array('id' => 'all', 'text' => 'all india');                 $source_array[1] = array('id' => 'ap', 'text' => 'andhra pradesh');                 $source_array[2] = array('id' => 'arp', 'text' => 'arunachal pradesh');                 $source_array[3] = array('id' => 'asm', 'text' => 'assam');                 $source_array[4] = array('id' => 'bhr', 'text' => 'bihar');                 $source_array[5] = array('id' => 'dl', 'text' => 'delhi');                  echo tep_draw_pull_down_menu('data[source][]', $source_array,'','class="srcinfo_source" multiple onchange="changeselection(this.value)" id="ind"'); ?>          </td>         <td width="10%">         <?php   $frequency_array = array();                 $frequency_array[0] = array('id' => 'yr', 'text' => 'yearly');                 $frequency_array[1] = array('id' => 'mnth', 'text' => 'monthly');                   echo tep_draw_pull_down_menu('data[freq][]', $frequency_array,'','class="srcinfo" style="margin-top:50px;"'); ?>          </td>         <td width="8%">         <?php echo tep_draw_input_field('data[qty][]','','class="srcinfo" style="margin-top:50px;"'); ?>          </td>         <td width="10%">         <?php   $unit_array = array();                 $unit_array[0] = array('id' => 'kg', 'text' => 'kilogram');                 $unit_array[1] = array('id' => 'gr', 'text' => 'gram');                 $unit_array[2] = array('id' => 'mg', 'text' => 'milligram');                   echo tep_draw_pull_down_menu('data[unit][]', $unit_array,'','class="srcinfo" style="margin-top:50px;"'); ?>          </td>     </tr>  </table>        </td></tr>                             <tr><td>                             <?php echo tep_draw_button(image_button_prodinfo, 'person', null, 'primary',array( 'params' => 'name="add_pro" class="btn btn-danger" ')); ?>                             </td></tr>                         </table>                         <?php                      if(isset($_post['add_pro']))                         {                         foreach($html->find('datatable') $table){                           $all_trs = $table->find('tr');                          $count = count($all_trs);                          echo $count;                          exit;                         }                         $src = $_post['source'];                         $products_source = implode(",", $src);                         $opt = $_post['option'];                         $products_options = implode(",", $opt);                         $products_array = array(                           'products_quantity' => $products_quantity,                           'products_date_added' => 'now()',                           'products_source' => $products_source,                           'products_frequency'  => $products_frequency,                           'products_options' => $products_options,                           'products_unit' => $products_unit,                           'customers_id' => $customer_id                           );                          tep_db_perform(table_products, $products_array);                             $products_desc_array = array(                           'products_name' => $products_name,                           'products_description' => $products_description                           );                          tep_db_perform(table_products_description, $products_desc_array);                         $prod_id = tep_db_insert_id();                         $category_id_array = array('products_id' => $prod_id,                                                     'categories_id' => $category_type_id);                         tep_db_perform(table_products_to_categories, $category_id_array);                         $update_stage = tep_db_query("update " . table_customers . " set customers_stage='5' customers_id = '" . $customer_id. "'");                         tep_redirect(tep_href_link(filename_registration_6, '', 'ssl'));                         }                      ?>                     </form> 

i want insert values of rows in form generated dynamically.

you can jquery, try this

$(document).ready(function () {     var dt = $(".table_class tr").length;     alert(dt); }); 

check in action here http://jsfiddle.net/g3dtu/


Comments

Popular posts from this blog

javascript - Count length of each class -

What design pattern is this code in Javascript? -

hadoop - Restrict secondarynamenode to be installed and run on any other node in the cluster -