将HTML <td>标记值获取到PHP变量或JS变量

时间:2019-12-12 07:20:41

标签: javascript php html

请参见以下代码

<table id="datatable" class="table table-striped table-bordered">
                            <thead>
                            <tr>
                               <th>Consignee Name</th>
                               <th>Reference Code</th>
                               <th>Consignee Number</th>
                               <th>Consignee Email</th>
                               <th>Consignee Address</th>
                               <th>COD</th>
                               <th>Pieces</th>
                               <th>Wieght</th>
                               <th>City</th>
                            </tr>
                            </thead>


                            <tbody>
                                <?
                                    foreach($sheet_data as $sheet){
                                ?>
                                    <tr>
                                        <td><?=$sheet['C']?></td>
                                        <td><?=$sheet['D']?></td>
                                        <td><?=$sheet['E']?></td>
                                        <td><?=$sheet['F']?></td>
                                        <td><?=$sheet['G']?></td>
                                        <td><?=$sheet['H']?></td>
                                        <td><?=$sheet['I']?></td>
                                        <td><?=$sheet['J']?></td>
                                        <td>
                                            <select class="form-control select2" name="city_id">
                                                <?php
                                                    foreach ($cities as $city) {
                                                    ?>
                                                    <option value="<?=$city['ref_id']?>" <?
                                                    if($sheet['L']==$city['name'])
                                                    echo 'selected';
                                                    ?>><?=$city['name']?></option>
                                                    <?php
                                                    }
                                                    ?>
                                            </select>
                                        </td>
                                    </tr>
                                    <?
                                    }
                                    ?>
                            </tbody>
                        </table>

我想获取每个数组的数据。 我不知道该怎么做。 它是一个大数据,如100多个行。 在td中也有一个选择输入,我想将其输入值

制成的数组应该是JavaScript,这样我就可以通过ajax将值发布到控制器(PHP)

2 个答案:

答案 0 :(得分:0)

我们可以使用Javascript或jQuery轻松地做到这一点。我正在遵循jQuery方法。参见下面的示例。

 // Perform some action on button click
jQuery('#getValues').click(function(){
//We will iterate over all rows in inside tbody of table with id myTable 
  jQuery('#myTable tbody tr').each(function(index, tr) {
         var lines =jQuery('td', tr).map(function(index, td) {
          return jQuery(td).text();
// This will save all date in lines array
    });
  
// Now test lines array by doing a console.log()
      console.log(lines[0] + ' ' + lines[1] + ' '  + lines[2] + ' '  + lines[3] );
    })
    // Now you have all values in lines array. You can send this complete array via ajax to server.
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table id="myTable" border="1">
  <thead>
     <tr>
      <th> Col1 </th>
      <th> Col2 </th>
      <th> Col3 </th>
      <th> Col4 </th>
    </tr>
  </thead>

<tbody>
<tr>
  <td> Value 11 </td>
  <td> Value 12 </td>
  <td> Value 13</td>
  <td> Value 14 </td>
</tr>

<tr>
  <td> Value 21 </td>
  <td> Value 22 </td>
  <td> Value 23</td>
  <td> Value 24 </td>
</tr>
<tr>
  <td> Value 31 </td>
  <td> Value 32 </td>
  <td> Value 33</td>
  <td> Value 34 </td>
</tr>
<tr>
  <td> Value 41 </td>
  <td> Value 42 </td>
  <td> Value 43</td>
  <td> Value 44 </td>
</tr>
</tbody>

</table>

<button id="getValues"> Get all values </button>

所有必要的注释都添加到上述代码中。您可以根据需要修改示例。

答案 1 :(得分:0)

<table id="datatable" class="table table-striped table-bordered">
                            <thead>
                            <tr>
                               <th>Consignee Name</th>
                               <th>Reference Code</th>
                               <th>Consignee Number</th>
                               <th>Consignee Email</th>
                               <th>Consignee Address</th>
                               <th>COD</th>
                               <th>Pieces</th>
                               <th>Wieght</th>
                               <th>City</th>
                            </tr>
                            </thead>


                            <tbody>
                                <form action="<?=base_url()?>client/save_excel_date" method="POST">
                                <?
                                $count=1;
                                    foreach($sheet_data as $sheet){
                                ?>
                                    <tr>
                                        <td><input class="form-control" name="sheetc[a<?=$count?>]" value="<?=$sheet['C']?>"></td>
                                        <td><input class="form-control" name="sheetd[a<?=$count?>]" value="<?=$sheet['D']?>"></td> 
                                        <td><input class="form-control" name="sheete[a<?=$count?>]" value="<?=$sheet['E']?>"></td>
                                        <td><input class="form-control" name="sheetf[a<?=$count?>]" value="<?=$sheet['F']?>"></td>
                                        <td><input class="form-control" name="sheetg[a<?=$count?>]" value="<?=$sheet['G']?>"></td>
                                        <td><input class="form-control" name="sheeth[a<?=$count?>]" value="<?=$sheet['H']?>"></td>
                                        <td><input class="form-control" name="sheeti[a<?=$count?>]" value="<?=$sheet['I']?>"></td>
                                        <td><input class="form-control" name="sheetj[a<?=$count?>]" value="<?=$sheet['J']?>"></td>
                                        <td>
                                            <select class="form-control select2" name="city_id[a<?=$count?>]">
                                                <?php
                                                    foreach ($cities as $city) {
                                                    ?>
                                                    <option value="<?=$city['ref_id']?>" <?
                                                    if($sheet['L']==$city['name'])
                                                    echo 'selected';
                                                    ?>><?=$city['name']?></option>
                                                    <?php
                                                    }
                                                    ?>
                                            </select>
                                        </td>
                                    </tr>
                                    <?
                                    $count++;
                                    }
                                    ?>
                                    <input type="submit" value="Save">
                                    </form>
                            </tbody>
                        </table>

然后在控制器中

$data   =   $this->input->post();
    $consignee_name     =   $data['sheetc'];
    $consignee_ref     =   $data['sheetd'];
    $consignee_number     =   $data['sheete'];
    $consignee_email     =   $data['sheetf'];
    $consignee_address     =   $data['sheetg'];
    $cod     =   $data['sheeth'];
    $pcs     =   $data['sheeti'];
    $weight     =   $data['sheetj'];
    $city_id     =   $data['city_id'];


    $arrays     =   array_merge_recursive($consignee_name,$consignee_ref,$consignee_number,$consignee_email,$consignee_address,$cod,$pcs,$weight,$city_id);
    print("<pre>".print_r($arrays,true)."</pre>");die;