从多个表获取PHP复选框数据

问题描述 投票:0回答:2

首先我会告诉我想要实现什么,然后我会解释我是如何尝试做到这一点的。

我有两张表,一张存储船只类型及其描述和自己的 ID。然后我有另一个表,其中容器类型已存储为文本。我的目标是选择每一个(两个表中的两条记录),并在两个表中都带有复选框,并将表 1 中的 Id 存储在表 2 中。

我会介绍数据,以提供帮助。

表1

 1|Balandra|Some description
 2|Bergantin|Some description
 3|Whatever |Whatever.....

表2

Balandra
Bergantin
Whatever

然后,我创建了一个 PHP 页面,显示两个带有我上面提到的复选框的表。复选框存储 Table1 Id 和 Table2 容器类型名称。

<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
循环。 预先感谢。

php checkbox
2个回答
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
投票

最后我用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>

我希望这有帮助。一如既往地感谢您的反馈。

© www.soinside.com 2019 - 2024. All rights reserved.