dsjuimtq920056 2016-05-11 10:34
浏览 34
已采纳

许多提交按钮用于相同的html表单

hope you fine and well,

i have the following form:

<form class="hidden-print"  method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">                                  
                 <table class="table table-bordered">
                    <thead>         
                      <tr>
                      <th>ID</th>
                      <th>Name</th>                 
                      </tr>
                    </thead>
                <tr>
                 <?php           
                     $pdo = Database::connect();
                     $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);          
                     $sql = "SELECT * from persons";             
                     $i=0;           
                     foreach ($pdo->query($sql) as $row) {
                            echo '<tr>';
                            echo '<td>'. $row['id'] . '</td>';
                            echo'<input type="hidden" name="id[]" value="' . $row['id'] . '    ">';                                                       
                            echo '<td>'. $row['Name'] . '</td>'; 
                            echo'<input type="hidden" name="name[]" value="' . $row['Name'] . '    ">';                                                                             
                            $i++;                          
                    }
                   Database::disconnect();                 
                   ?>                                                   
            </tr> 
            </table>
                 <input class="btn btn-primary" type="submit" value="Send" >
            </form> 

let me explain what happening here, my form is PHP_SELF so i post the data to the same page, i created a table inside the form which contains ID's and Names, i loop through the selected data and each row in the table will contain id and name of person, as you see, i defined the inputs name and id as arrays eg. name[] and id[] , and i have a submit button at the end of the form will post the arrays id[] and name[].

now what i want is to put an submit input in each row ! so when i click the submit button it will post just the id and the name of the person in this row !

is this possible ?!

regards.

  • 写回答

1条回答 默认 最新

  • doxrxwn2252 2016-05-11 10:38
    关注

    You are already using associative array in names e.g. name[]

    Use counters:

    name[<?php echo $i;?>]

    And add submit buttons with the same

    <input type="submit" name="submit[<?php echo $i;?>]"/>

    In PHP, loop over $_POST.

    And check index of submit button using foreach loop.

    Use the same index for name, id, etc.

    Hope it helps.

    Answer updated with code:

    <form class="hidden-print"  method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
    <table class="table table-bordered">
    <thead>
    <tr>
    <th>ID</th>
    <th>Name</th>
    </tr>
    </thead>
    <tr>
    <?php
    $pdo = Database::connect();
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $sql = "SELECT * from persons";
    $i=0;
    foreach ($pdo->query($sql) as $row) {
     echo '<tr>';
     echo '<td>'. $row['id'] . '</td>';
     echo'<input type="hidden" name="id['.$row['id'].'" value="' . $row['id'] . '    ">';
     echo '<td>'. $row['Name'] . '</td>';
     echo'<input type="hidden" name="name['.$row['id'].'" value="' . $row['Name'] . '    ">';
     ?>
     <td>
     <input class="btn btn-primary" type="submit" value="Send" name="send<?php echo $rowp['id'];?>" >
     </td>
     </tr>
     <?php
        ++$i;
    }
    Database::disconnect();
    ?>
                </tr> 
                </table>
                </form> 
    <?php
    if (! empty($_POST['send'])) {
     foreach ($_POST['send'] as $key => $posted) {
      $id = isset($_POST['id'][$key]) ? $_POST['id'][$key] : '';
      $name = isset($_POST['name'][$key]) ? $_POST['name'][$key] : '';
      // Do your SQL here.
     }
    }
    ?>
    

    Note: HTML mark ups are used as it is, please correct it as per your needs.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来