当我在表中编辑一行时,我第一次使用模态。对于我单击编辑的行。
我希望这是有道理的。
这是我要回应的行
<h5 class="modal-title" id="allocate">Allcoate Project to <?php echo $row['id'] ?></h5>
这是我的代码
<?php
include ('../resources/styling.html');
include ('adminNavbar.php');
require ('../resources/config.php');
?>
<div class="container content-area">
<div class="row">
<div class="col-md-12">
<div class="panel">
<br>
<div class="text-center border border-light p-5">
<p class="h4 mb-4">Student Project Choice Submissions</p>
</div>
<table class="table table-hover">
<thead>
<tr>
<th scope="col">Full Name</th>
<th scope="col">First Choice</th>
<th scope="col">Second Choice</th>
<th scope="col">Third Choice</th>
<th scope="col">Allocation</th>
</tr>
</thead>
<tbody>
</div>
</div>
</div>
</div>
<?php
$sql = "SELECT * FROM students";
$result = $db->query($sql);
while($row = mysqli_fetch_array($result))
{
?>
<tr>
<td><?php echo $row["firstname"], ' ' .$row["surname"]; ?></td>
<td><?php echo $row["firstchoice"]; ?></td>
<td><?php echo $row["secondchoice"]; ?></td>
<td><?php echo $row["thirdchoice"]; ?></td>
<td><input type="button" name="allocate" value="allocate" id="<?php echo $row["id"]; ?>" class="btn btn-info btn-xs allocate" /></td>
</td>
</tr>
<?php
}
?>
<!-- Modal -->
<div class="modal fade" id="allocate" tabindex="-1" role="dialog" aria-labelledby="allocate" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="allocate">Allcoate Project to <?php echo $row['id'] ?></h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<!-- Modal -->
<form action="insertcode.php?id=<?php $row['id'] ?>" method="post" enctype="multipart/form-data" novalidate>
<div class="modal-body">
<div class="form-row mb-4">
<div class="col">
<label for="programme">Student Submission</label>
<br>
<select class="form-control" name = "allocatedproject" id="allocatedproject" required>
<option value=""> --Select Course--</option>
<option value="Web & Mobile Development"> Web & Mobile Development</option>
<option value="Computer Science"> Computer Science</option>
</select>
<div class="valid-feedback">
Looks good!
</div>
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary" name="submit">Allocate Project</button>
</div>
</div>
</div>
</div>
</form>
<!-- Modal -->
<script>
$(document).ready(function() {
$('.allocate').on('click', function(){
$('#allocate').modal('show');
});
});
</script>
您可以使用此:
$(document).ready(function() {
$('.allocate').on('click', function(){
id = $(this).attr('id');
$('#allocate').modal('show');
// Change Modal Title
$('.modal-title').eq(0).html("Allocate Project to "+id);
// Change modal form action
$('.modal-content form').eq(0).attr("action","insertcode.php?id="+id);
});
});
但是它似乎并没有确认我单击编辑的行的任何ID。
那是因为当您这样做时:
Allcoate Project to <?php echo $row['id'] ?>
$row['id']
的值来自while循环的最终迭代