0

我遇到了一个弹出表单的问题,该表单有一个字段,用户需要在其中输入有关一行数据的一些信息。

他/她输入的内容需要插入到我将执行更新查询的表中......但我的问题是表单回显是源代码中数据库中的 id 但当我将鼠标悬停在按钮。

在我按下按钮后,它只执行第一个 id 的查询。其他只显示第二个,依此类推,直到它们被一一删除。我不知道我做错了什么:

    <?php include_once('classes/profile.class.php');?>
<?php include_once('header.php');?>

<h1>

    <?php _e('Traffic Fines Adjudication'); ?>

</h1>

<br>

<div class="tabs-left">
    <?php

    $result = "SELECT * FROM login_fines_adjudicated WHERE active = 0 ORDER BY date_issued";
    $stmt = $generic->query($result);

//this function will take the above query and create an array
while($row = $stmt->fetch(PDO::FETCH_ASSOC))
  {
        //do nothing
 //with the array created above, I can create variables (left) with the outputted array (right)
    $id = $row['id'];
    $date = $row['date_added_db'];
    $date_issued = $row['date_issued'];
    $time_arrive = $row['time_arrived'];
    $time_depart = $row['time_departed'];
    $ref = $row['reference_code'];
    $reason = $row['violation_reason'];
    $location = $row['location'];
    $bay = $row['bay_number'];
    $licence = $row['licence'];
    $punit = $row['parking_unit'];
    $value = $row['value'];
    $operator = $row['operator'];
    $operator_id = $row['operator_id'];


    //If date field is empty display nothing! Or sentence...

    if(!empty($date))
    {
?>

    <table class="table">
        <thead>
        <tr>
            <th>Photo</th>
            <th>Date Added</th>
            <th>Date Issued</th>
            <th>Time Arrived</th>
            <th>Time Departed</th>
            <th>Reference Code</th>
            <th>Violation Category</th>
            <th>Location</th>
            <th>Bay Number</th>
            <th>Licence Plate</th>
            <th>Parking Unit</th>
            <th>Amount</th>
            <th>Operator Name</th>
            <th>Operator ID</th>
            <th>Action</th>
        </tr>
        </thead>
        <tr>
            <td><img class="photo" src="photos/no-available.jpg" /></td>
            <td><?php echo $date; ?><br /></td>
            <td><?php echo $date_issued; ?></td>
            <td><?php echo $time_arrive; ?></td>
            <td><?php echo $time_depart; ?></td>
            <td><?php echo $ref; ?></td>
            <td><?php echo $reason; ?></td>
            <td><?php echo $location; ?></td>
            <td><?php echo $bay; ?></td>
            <td><?php echo $licence; ?></td>
            <td><?php echo $punit; ?></td>
            <td><?php echo $value; ?></td>
            <td><?php echo $operator; ?></td>
            <td><?php echo $operator_id; ?></td>
            <td>
            <form action="approve.php" method="post">
            <input type="hidden" name="delete_id" value="<?php echo $row['id']; ?>" />
            <p><a href='approve.php?id=<?php echo $id; ?>'><button url="#" class="btn btn-primary">Approve</button></a></p>
            </form>
                    <div id="reject-form" class="modal hide fade">
                        <div class="modal-header">
                            <a class="close" data-dismiss="modal">&times;</a>
                            <h3><?php _e('Reject Reason'); ?></h3>
                        </div>
                        <div class="modal-body">
                            <div id="message"></div>
                    <form action="reject.php" method="post" name="rejectform" id="rejectform" class="form-stacked rejectform normal-label">
                        <div class="controlgroup rejectcenter">
                                <div class="control">
                                    <input id="reject" name="reject" type="text"/>
                                </div>
                        </div>
                    <input type="hidden" name="delete_id" value="<?php echo $id; ?>" />
                    </form>
                </div>
                <div class="modal-footer">
                    <a href='reject.php?id=<?php echo $id; ?>'><button data-complete-text="<?php _e('Done'); ?>" class="btn btn-primary pull-right" id="rejectsubmit"><?php _e('Submit'); ?></button></a>
                    <p class="pull-left"><?php _e('Please give a short reason for rejecting.'); ?></p>
                    </div>
                </div>
                </form>
            <p><a data-toggle="modal" href="#reject-form" id="rejectlink" tabindex=-1><button url="#" class="btn btn-primary">Reject</button></a></p>
            </td>
        </tr>
    </table>


<?php 
}
}
?>
</div>

<?php include ('footer.php'); ?>

任何帮助,将不胜感激!

4

1 回答 1

0

问题是您有许多具有相同id属性的元素。一般来说,拥有多个相同的元素是一种非常糟糕的做法,id更不用说它不符合HTML 规范,根据该规范,“id = [...] 这个名称在文档中必须是唯一的。” . 因此,强烈建议您修复所有 ID,确保它是唯一的(例如,附加当前条目的 ID)。

导致您在问题中描述的特定问题的原因是:

  1. 您使用 id 为每个表定义一个弹出对话框 ( div) reject-form
  2. 然后,您将“拒绝”按钮实现为与href="#reject-form".
  3. 因此,当您单击任何“拒绝”按钮时,用户将被带到 DOM 中找到的第一个元素id="reject-form"(始终是第一个条目的对话框)。

为了解决这个问题,您可以id在两个位置的属性中附加每个条目的 ID:弹出对话框和“拒绝”按钮:

// Replace that:
<div id="reject-form" class="modal hide fade">
// with this:
<div id="reject-form-<?php echo $id; ?>" class="modal hide fade">

// Replace that:
<a data-toggle="modal" href="#reject-form" id="rejectlink" tabindex=-1>
// with this:
<a data-toggle="modal" href="#reject-form-<?php echo $id; ?>" id="rejectlink" tabindex=-1>
于 2013-05-31T13:18:21.417 回答