如何使用按钮将值动态传递给我的模态?
How can I dynamic pass value to my modal with button?
<table class="table">
<tr>
<td colspan="12" class="text-center bg-primary text-white" style="font-size:20px;">List all people</td>
</tr>
<?php
if($result->num_rows>0){
while($row=$result->fetch_assoc()){
$people=new People;
$people->id=$row['id'];
$people->firstname=$row['firstname'];
$people->lastname=$row['lastname'];
$people->email=$row['email'];
$people->registerdate=$row['reg_date'];
echo "<tr>
<td style='width:60px;' name='id'>$people->id<td>
<td>$people->firstname<td>
<td>$people->lastname<td>
<td>$people->email<td>
<td>$people->registerdate<td>
<td><button class='btn btn-danger w-100' data-toggle='modal' data-target='#mymodal'>Action</button><td>
<tr>";
require('modal.php');
}
}
?>
<div class="modal fade" id="mymodal">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title"><?php echo "User : $people->firstname $people->lastname" ?></h4>
</div>
<div class="modal-body">
<label for="">User ID</label>
<input type="text" class="form-control" value="<?php echo $people->id ?>">
<label for="">First name</label>
<input type="text" class="form-control" value="<?php echo $people->firstname ?>">
<label for="">Last name</label>
<input type="text" class="form-control" value="<?php echo $people->lastname ?>">
</div>
<div class="modal-footer">
modal footer
</div>
</div>
</div>
</div>
</table>
我正在使用 jQuery 用于此目的。
<table class="table">
<tr>
<td colspan="12" class="text-center bg-primary text-white" style="font-size:20px;">List all people</td>
</tr>
<?php
if($result->num_rows>0)
{
$serial_no = 0;
while($row=$result->fetch_assoc())
{
$serial_no++;
$people=new People;
$people->id=$row['id'];
$people->firstname=$row['firstname'];
$people->lastname=$row['lastname'];
$people->email=$row['email'];
$people->registerdate=$row['reg_date'];
echo "<tr>
<td style='width:60px;' name='id'>$people->id<td>
<td>$people->firstname<td>
<td>$people->lastname<td>
<td>$people->email<td>
<td>$people->registerdate<td>";
?>
<td>
<button class='btn btn-danger w-100' data-toggle='modal' onclick="open_modal('<?php echo $people->id; ?>', '<?php echo $people->firstname; ?>', '<?php echo $people->lastname; ?>');" data-target='#mymodal'>Action</button>
<td>
<tr>
<?php
require('modal.php');
}
}
?>
</table>
<div class="modal fade" id="mymodal">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title"><?php echo "User : $people->firstname $people->lastname" ?></h4>
</div>
<div class="modal-body">
<label for="">User ID</label>
<input type="text" class="form-control" id="people_id">
<label for="">First name</label>
<input type="text" class="form-control" id="people_firstname">
<label for="">Last name</label>
<input type="text" class="form-control" id="people_lastname">
</div>
<div class="modal-footer">
modal footer
</div>
</div>
</div>
</div>
<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
<script>
function open_modal(id, first_name, last_name)
{
$("#people_id").val (id);
$("#people_firstname").val (first_name);
$("#people_lastname").val (last_name);
$("#mymodal").modal("show");
}
</script>
<table class="table">
<tr>
<td colspan="12" class="text-center bg-primary text-white" style="font-size:20px;">List all people</td>
</tr>
<?php
if($result->num_rows>0){
while($row=$result->fetch_assoc()){
$people=new People;
$people->id=$row['id'];
$people->firstname=$row['firstname'];
$people->lastname=$row['lastname'];
$people->email=$row['email'];
$people->registerdate=$row['reg_date'];
echo "<tr>
<td style='width:60px;' name='id'>$people->id<td>
<td>$people->firstname<td>
<td>$people->lastname<td>
<td>$people->email<td>
<td>$people->registerdate<td>
<td><button class='btn btn-danger w-100' data-toggle='modal' data-target='#mymodal'>Action</button><td>
<tr>";
require('modal.php');
}
}
?>
<div class="modal fade" id="mymodal">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title"><?php echo "User : $people->firstname $people->lastname" ?></h4>
</div>
<div class="modal-body">
<label for="">User ID</label>
<input type="text" class="form-control" value="<?php echo $people->id ?>">
<label for="">First name</label>
<input type="text" class="form-control" value="<?php echo $people->firstname ?>">
<label for="">Last name</label>
<input type="text" class="form-control" value="<?php echo $people->lastname ?>">
</div>
<div class="modal-footer">
modal footer
</div>
</div>
</div>
</div>
</table>
我正在使用 jQuery 用于此目的。
<table class="table">
<tr>
<td colspan="12" class="text-center bg-primary text-white" style="font-size:20px;">List all people</td>
</tr>
<?php
if($result->num_rows>0)
{
$serial_no = 0;
while($row=$result->fetch_assoc())
{
$serial_no++;
$people=new People;
$people->id=$row['id'];
$people->firstname=$row['firstname'];
$people->lastname=$row['lastname'];
$people->email=$row['email'];
$people->registerdate=$row['reg_date'];
echo "<tr>
<td style='width:60px;' name='id'>$people->id<td>
<td>$people->firstname<td>
<td>$people->lastname<td>
<td>$people->email<td>
<td>$people->registerdate<td>";
?>
<td>
<button class='btn btn-danger w-100' data-toggle='modal' onclick="open_modal('<?php echo $people->id; ?>', '<?php echo $people->firstname; ?>', '<?php echo $people->lastname; ?>');" data-target='#mymodal'>Action</button>
<td>
<tr>
<?php
require('modal.php');
}
}
?>
</table>
<div class="modal fade" id="mymodal">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title"><?php echo "User : $people->firstname $people->lastname" ?></h4>
</div>
<div class="modal-body">
<label for="">User ID</label>
<input type="text" class="form-control" id="people_id">
<label for="">First name</label>
<input type="text" class="form-control" id="people_firstname">
<label for="">Last name</label>
<input type="text" class="form-control" id="people_lastname">
</div>
<div class="modal-footer">
modal footer
</div>
</div>
</div>
</div>
<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
<script>
function open_modal(id, first_name, last_name)
{
$("#people_id").val (id);
$("#people_firstname").val (first_name);
$("#people_lastname").val (last_name);
$("#mymodal").modal("show");
}
</script>