HTML/PHP 对 table 的每一行进行内联更新和删除

HTML/PHP inline update and delete for each row of a table

内联 'update' 按钮有点问题,我从 mysql table 中提取数据(完成),使用 'foreach'(完成)并在每一行都有一个更新和删除按钮,发布正确的按钮 'name' 和用户 ID(完成)。

但是 $_POST 数组没有解析的是带有行信息的正确数据....它确实解析了对底行的更改,但是如果您更改另一行,它只会再次解析最后一行.. ..

    <input type="hidden" name="action" value="submit" />
    <pre></pre>
    <?php print_r($_POST);?>
    <?php 
    echo '<table class="table table-condensed">';
    echo '<thead><tr><th style="width: 15%">Name</th><th style="width: 25%">Login</th><th style="width: 25%">Email</th><th style="width: 7%">Role</th><th style="width: 7%">Group</th><th style="width: 15%">LoftID</th><th>Edit</th><th>Delete</th></tr></thead><tbody>';
        foreach($users as $person){
             echo '<tr>';
             echo '<td><input type="text" class="form-control" name="name" id="'.$person['id'].'" placeholder="Name" value="'.$person['name'].'"/></td>';
             echo '<td><input type="text" class="form-control" name="login" id="'.$person['id'].'" placeholder="Login" value="'.$person['login'].'"/></td>';
             echo '<td><input type="text" class="form-control" name="mail" id="'.$person['id'].'" placeholder="Mail" value="'.$person['mail'].'"/></td>';
             echo '<td><input type="text" class="form-control" name="role" id="'.$person['id'].'" placeholder="Role" value="'.$person['role'].'"/></td>';
             echo '<td><input type="text" class="form-control" name="group" id="'.$person['id'].'" placeholder="Group" value="'.$person['group'].'"/></td>';
             echo '<td><select type="text" class="form-control" name="LoftID" id="'.$person['id'].'" placeholder="Loft">';
             foreach($lofts as $info) {
                 if($info['id']==$person['LoftID']){
                      echo '<option value='.$info["id"].' selected>'.$info["name"].'</option>';
                 }
                 else{
                      echo '<option value='.$info["id"].'>'.$info["name"].'</option>';    
                 }
             }
             echo '</select></td>';
             echo '<td><button type="submit" name="update" value="'.$person['id'].'" class="btn btn-primary">Update</button></td>';
             echo '<td><button type="submit" name="delete" value="'.$person['id'].'" class="btn btn-primary">Delete</button></td>';
             echo '</tr>';
          }   
          echo '</tbody></table>';
    ?>
    </div>
    </form>

我需要的是允许我从 $_POST 中提取数据,这样我就可以将它作为基于 ID 的更新返回到数据库中,我相信我可以做到,正如我所做的那样已经在我的代码中....

我真的很想这样做而不需要显示另一个页面上的数据来更新....

提前致谢

您需要 Ajax 才能在不重新加载页面或使用参数转到另一个页面的情况下完成这项工作。请记住,按钮不像输入那样具有价值。使用 data-id 或 data-pid 或任何你喜欢的。并输入一个 class 名称,例如 'updateBtn' 来标识按钮。

echo '<td><button type="button" data-pid="'.$person['id'].'" class="btn btn-primary updateBtn">Update</button></td>';

    <script>  
        $('.updateBtn').on('click', function() { 
        var pid = $(this).data('pid'); console.log(pid); // see the result in console
        var pid = parseInt(pid);  // change to int
        $.ajax({
                url: '/ajax/update-data/index.php', // specify folder and file name
                data: { pid : pid },
                dataType: 'json',
                success: function (response) {                                  
                if(response.status == 'success') {  

                   // do something to the html code to show the successful update
                return false;
                }   
               if(response.status == 'fail') {  

                   // do something to the html code to show the fail update
                return false;
                }   
              }   
           });        
        });
    </script>

Ajax 文件:

<?php

  function response ($status) { 
      header("Content-Type:application/json");
      $response['status'] = $status;
      $json_response = json_encode($response);
      echo $json_response;
  }

if($_POST) {

$pid = filter_input(INPUT_POST, 'pid', FILTER_SANITIZE_NUMBER_INT); 

    if($pid) {

        $done = // do php to chage update. You can refer to class or direct php mysql update

         if($done) {
          response ('success');
         }else{
          response ("fail");         
         }

    }else{
    response ("parameter fail");    

    }
}
?>

好的,我想我找到了答案,并包含以下内容;

echo '<form class="form-horizontal" action="" method="post">';

就在第一个 foreach 语句之下,因此每一行都是它自己的表单元素。

有没有人发现这样做有什么问题??数据总是非常有限,可能永远不会超过 10 行。

谨致问候并感谢所有帮助