2017-04-25 54 views
1

首先我會告訴我想實現什麼,然後我會解釋我是如何嘗試這樣做的。 我有兩張表,一個商店類型的船隻有一個描述和他們自己的Id。然後我有另一個表格,其中船隻類型已被存儲爲文本。我的目標是用兩個複選框選擇每一個(兩個表中都有記錄),並在表2中存儲表1中的Id。 我將介紹數據以提供幫助。 表1
1|Balandra|Some description 2|Bergantin|Some description 3|Whatever |Whatever.....從多個表獲取php複選框數據

表2 Balandra Bergantin Whatever

然後,我已創建一個PHP頁面顯示與我上面提到的複選框兩個表。複選框存儲Table1 Id和Table2 vesseltypename。

<table class="table table-striped table-bordered table-list"> 
<thead> 
<tr> 
<th><em class="fa fa-cog"></em></th> 
<th class="hidden-xs">idtiponavio</th> 
<th>Tipo de Navío</th> 
<th>Descripción</th> 
<th>Agrupar</th> 
</tr> 
</thead> 
<tbody> 

<?php foreach ($naviosdyncoop as $key => $navio) { ?> 
        <tr> 
         <td align="center"> 
         <a href=<?php echo '../vista/modificar.php?id=' . 
    $navio['idtiponavio']; ?> class="btn btn-default"><em class="fa fa- 
    pencil"></em></a> 
         <a href=<?php echo '../datos/borrar.php?id=' . 
    $navio['idtiponavio']; ?> class="btn btn-default"><em class="fa fa- 
    trash"></em></a> 
    </td> 
    <td class="hidden-xs"><?php echo 
    $navio['idtiponavio']; ?></td> 
    <td><?php echo $navio['tiponavio']; ?></td> 
    <td><?php echo $navio['descripcion']; ?></td> 
    <td><input type="checkbox" name="agruparid" value=<? 
    php echo $navio['idtiponavio']; ?> /></td> 

        </tr> 
       <?php } ?> 

      </tbody> 
      </table> 
     </div> 

     </div> 
     <div class="col-md-6"> 
<div class="panel-body paneltodo"> 
      <table class="table table-striped table-bordered table-list"> 
      <thead> 
       <tr> 
       <th><em class="fa fa-cog"></em></th> 
       <th>Tipo de Navío</th> 
       <th>Agrupar</th> 
       </tr> 
      </thead> 
      <tbody> 

       <?php foreach ($naviosforsea as $key => $navio) { ?> 
        <tr> 
         <td align="center"> 
         <a href=<?php echo $navio['typevessel']; ?> class="btn btn-default"><em class="fa fa-arrow-circle-o-left"></em></a> 
         </td> 
         <td><?php echo $navio['typevessel']; ?></td> 
         <td><input type="checkbox" name="agruparvessel" value=<?php echo $navio['typevessel']; ?> /></td> 

        </tr> 
       <?php } ?> 

      </tbody> 
      </table> 

因此,我想檢查兩個表記錄並將table1 Id存儲在table2 idtypevessel字段中。 我認爲,一個PHP文件可以存儲兩個項目,並呼籲與這些參數的更新功能,像這樣:

<?php 
require './modelo.php'; 
$idnavio = $_GET['agruparid']; 
$vessel = $_GET['agruparvessel']; 

任何建議,因爲我認爲我必須做一個按鈕來提交這個參數,但它必須在兩張桌子上工作,我不知道如何同時訪問兩個foreach循環。 在此先感謝。

+0

請刪除postgres標籤 –

+0

完成!我認爲這可能是有用的,因爲我連接到PostgreSQL數據庫,但實際上是不必要的。謝謝! –

回答

1

E.薩拉斯

審查波紋管參考代碼提交多選擇複選框值 多選擇複選框提交必須在HTML中使用[]運營商的名稱屬性

的index.php

<form action="/checkbox.php" method="post"> 
    <strong>Cars:</strong><br> 
    <?php 
    $cars = array("Volvo", "BMW", "Toyota"); 
    $colors = array("Red", "Green", "Black"); 
    foreach($cars as $single){ 
     ?> 
     <input type="checkbox" name="cars[]" value="<?php echo $single; ?>"> 
     <?php 
    } 
    <br> 
    <strong>colors:</strong><br> 
    foreach($colors as $single){ 
     ?> 
     <input type="checkbox" name="colors[]" value="<?php echo $single; ?>"> 
     <?php 
    } 
    ?> 
    <br> 
    <input type="submit" value="Submit!"> 
</form> 

checkbox.php

<?php 
echo "<pre>"; 
var_dump($_POST); 
exit; 

你的情況:

<form action="/checkbox.php" method="post"> 
    <div class="col-md-6"> 
     <div class="panel-body"> 
      <table class="table table-striped table-bordered table-list"> 
       <thead> 
        <tr> 
         <th><em class="fa fa-cog"></em></th> 
         <th class="hidden-xs">idtiponavio</th> 
         <th>Tipo de Navío</th> 
         <th>Descripción</th> 
         <th>Agrupar</th> 
        </tr> 
       </thead> 
       <tbody> 
       <?php foreach ($naviosdyncoop as $key => $navio) { ?> 
        <tr> 
         <td align="center"> 
          <a href=<?php echo '../vista/modificar.php?id=' . $navio['idtiponavio']; ?> class="btn btn-default"><em class="fa fa-pencil"></em></a> 
          <a href=<?php echo '../datos/borrar.php?id=' . $navio['idtiponavio']; ?> class="btn btn-default"><em class="fa fa-trash"></em></a> 
         </td> 
         <td class="hidden-xs"><?php echo $navio['idtiponavio']; ?></td> 
         <td><?php echo $navio['tiponavio']; ?></td> 
         <td><?php echo $navio['descripcion']; ?></td> 
         <td><input type="checkbox" name="agruparid[]" value=<?php echo $navio['idtiponavio']; ?> /></td> 
        </tr> 
       <?php } ?> 
       </tbody> 
      </table> 
     </div> 
    </div> 
    <div class="col-md-6"> 
     <div class="panel-body"> 
      <table class="table table-striped table-bordered table-list"> 
       <thead> 
        <tr> 
         <th><em class="fa fa-cog"></em></th> 
         <th>Tipo de Navío</th> 
         <th>Agrupar</th> 
        </tr> 
       </thead> 
       <tbody> 
       <?php foreach ($naviosforsea as $key => $navio) { ?> 
        <tr> 
         <td align="center"> 
         <a href=<?php echo $navio['typevessel']; ?> class="btn btn-default"><em class="fa fa-arrow-circle-o-left"></em></a> 
         </td> 
         <td><?php echo $navio['typevessel']; ?></td> 
         <td><input type="checkbox" name="agruparvessel[]" value=<?php echo $navio['typevessel']; ?> /></td> 
        </tr> 
       <?php } ?> 

       </tbody> 
      </table> 
     </div> 
    </div> 
    <input type="submit" value="Submit!"> 
</form> 
+0

謝謝!但我的問題是我必須存儲兩個表格複選框,而且我不知道如何處理這兩個'foreach'循環。因爲你的解決方案只適用於其中一個循環,或者我錯過了什麼? –

+0

現在你可以使用多個foreach循環,但要確保你在名稱屬性如** name =「cars []」後傳遞數組操作** –

+0

謝謝! @ hirendrasinh-s-rathod,我有點傻。我如何使用多個foreach循環?因爲我在我的html中有單獨的表格,所以我如何從提交按鈕訪問這兩個複選框?因爲我同時需要兩個值。我實際上不知道你是否已經回答了我的問題:( –

0

最後我解決了這個用javascript。我的一位合作伙伴幫助了我,我發佈這個來幫助那些符合我的情況的人。 我創建了一個腳本,這裏是代碼:

<script type="text/javascript"> 

function cogeNavioDyncoopnet(){ 
var checkedValueNavioD = null; 
var inputElements = document.getElementsByClassName('checknaviod'); 
for(var i=0; inputElements[i]; ++i){ 
     if(inputElements[i].checked){ 
      checkedValueNavioD = inputElements[i].value; 
      break; 
     } 
    }//return checkedValueNavioD; 
    var input_nav_dyn = document.getElementById("nav_dyn"); 
     input_nav_dyn.value = checkedValueNavioD; 
} 
function cogeNavioForSea(){ 
var checkedValueNavioFs = null; 
var inputElements = document.getElementsByClassName('checknaviofs'); 
for(var i=0; inputElements[i]; ++i){ 
     if(inputElements[i].checked){ 
      checkedValueNavioFs = inputElements[i].value; 
      break; 
     } 
    }//return checkedValueNavioFs; 
    var input_nav_fs = document.getElementById("nav_fs"); 
     input_nav_fs.value = checkedValueNavioFs; 
} 
</script> 

然後創建以下形式,收集值並將其發送給我的.PHP控制文件。

<div class="hidden-xs"> 
<form class="hidden-xs" method="POST" 
action="../datos/actualizarnavios.php"> 
    <input id="nav_dyn" type="text" name="idnaviodyncoop"> 
    <input id="nav_fs" type="text" name="navioforsea" > 
    <input id="botonasignar" type="submit" name="enviardatosdynfs"> 
</form> 
</div> 

我希望這有助於。感謝反饋,一如既往。