表单仅更新第一个ID

时间:2016-08-28 10:25:35

标签: php html forms

这是Bootstrap弹出(淡入淡出)表单,我有所有成员的列表及其旁边的按钮,当您单击时,您可以编辑指定用户(id)的配置文件。一切正常,除了<div class="modal fade" id="myModal6" role="dialog">如果我删除它,然后我可以编辑每个用户,但有了它,它只更新第一个用户。

$query = $handler->query('SELECT * FROM users');
<?php while($r = $query->fetch()) { ?>
<div class="modal fade" id="myModal6" role="dialog">
          <div class="modal-dialog modal-sm">
                 <!-- Modal content-->
                 <div class="modal-content">
                        <div class="modal-header">
                            <button type="button" class="close" data-dismiss="modal">&times;</button>
                            <h4 class="modal-title">Izmeni</h4>
                        </div>
                        <div class="modal-body">
                            <p>
                                <form action="admin.php?update_id=<?php echo $r['id']; ?>" method="post">
                                    <div class="field">
                                        <center><input type="text" name="updatename" class="form-control" required="" placeholder="<?php echo $r['id']; ?>"></center><br>
                                        <center><input type="text" name="updatesurname" class="form-control" required="" placeholder="Prezime"></center><br>
                                        <center><button name="submit" type="submit" class="btn btn-block btn-success"><span class="glyphicon glyphicon-ok"></span> Izmeni</button></center>
                                    </div>
                                </form>
                            </p>
                        </div>
                    </div>
                </div>
            </div>
 <?php } ?>

所以,如果我删除<div class="modal fade" id="myModal6" role="dialog">它会起作用,但是我的表单会像这样被搞砸了。

enter image description here

2 个答案:

答案 0 :(得分:1)

而不是<div class="modal fade" id="myModal6" role="dialog">使用

<div class="modal fade" id="myModal<?php echo $r['id']; ?>" role="dialog">

元素的ID在整个页面中应该是唯一的。

答案 1 :(得分:1)

  如果需要正确执行,页面上的ID必须是唯一的。或者esle它会被打成棍棒,它会产生非强制性的结果。

列表页面

<a type="button" data-toggle="modal" data-target="#myModal<?php echo $r['id']; ?>" class="btn btn-xs btn-info" href="#"><i class="glyphicon glyphicon-edit"></i></a>

这将为所有弹出窗口创建单独的ID,您可以使用它。

修改后的代码:

$query = $handler->query('SELECT * FROM users');
<?php while($r = $query->fetch()) { ?>
<div class="modal fade" id="myModal<?php echo $r['id']; ?>" role="dialog">
          <div class="modal-dialog modal-sm">
                 <!-- Modal content-->
                 <div class="modal-content">
                        <div class="modal-header">
                            <button type="button" class="close" data-dismiss="modal">&times;</button>
                            <h4 class="modal-title">Izmeni</h4>
                        </div>
                        <div class="modal-body">
                            <p>
                                <form action="admin.php?update_id=<?php echo $r['id']; ?>" method="post">
                                    <div class="field">
                                        <center><input type="text" name="updatename" class="form-control" required="" placeholder="<?php echo $r['id']; ?>"></center><br>
                                        <center><input type="text" name="updatesurname" class="form-control" required="" placeholder="Prezime"></center><br>
                                        <center><button name="submit" type="submit" class="btn btn-block btn-success"><span class="glyphicon glyphicon-ok"></span> Izmeni</button></center>
                                    </div>
                                </form>
                            </p>
                        </div>
                    </div>
                </div>
            </div>
 <?php } ?>