2017-04-25 53 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> 

我希望这有助于。感谢反馈,一如既往。