gpt4 book ai didi

php - 如何在模态中传递当前行值?

转载 作者:可可西里 更新时间:2023-11-01 09:03:46 26 4
gpt4 key购买 nike

我正在对表执行 PHP CRUD 操作。当我单击编辑按钮而不是将其带到新页面时,我想在模式中显示值。我希望值以模式的形式显示。我创建了一个模式,但我想不出一种逻辑来传递单击编辑按钮的行的值。任何帮助将不胜感激。

表格:

<table class="table datatable-basic table-bordered table-hover">
<thead>
<tr class='active'>
<th><b>S.No.</b></th>
<th><b>Name</b></th>
<th><b>Mobile Number</b></th>
<th><b>Password</b></th>
<th><b>Actions</b></th>
</tr>
</thead>

<tbody>
<?php

$sql="SELECT * from users ORDER BY name ASC" ;
$c = 1;
$results = $result->query($sql);
while($row = $results->fetch_assoc())
{
echo '<tr style="font-weight:normal;">';
echo "<td>$c</td>";
echo "<td>{$row['name']}</td>";
echo "<td>{$row['username']}</td>";
echo "<td>{$row['pass']}</td>";

echo "<td class='text-center'><ul class='icons-list'><a href='#' style='color:#000;'><i class='icon-pencil5' data-toggle='modal' data-target='#modal_edit'
data-popup='tooltip' title='Edit' data-container='body'></i></a>

<a href='delete.php?teacherid={$row['username']}' style='color:#000;'><i class='icon-cross2' data-popup='tooltip' title='Delete'
data-container='body'></i></a></ul></td>";
echo '</tr>';
++$c;
}
?>
</tbody>
</table>

我的模态是这样的:-

<div id="modal_edit" class="modal fade" style="font-weight: normal;">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">&times;</button>
<h5 class="modal-title">Add Teacher</h5>
</div>

<form action="" method="POST">
<div class="modal-body">
<div class="form-group">
<div class="row">
<div class="col-sm-6">
<label>Full Name</label>
<input type="text" name="fullname" class="form-control" required>
</div>

<div class="col-sm-6">
<label>Mobile Number</label>
<input type="text" name="mobno" class="form-control" required>
</div>
</div>
</div>

<div class="form-group">
<div class="row">
<div class="col-sm-6">
<label>Password</label>
<input type="password" name="password" class="form-control" required>
</div>


</div>
</div>



<div class="modal-footer">
<button type="button" class="btn btn-link" data-dismiss="modal">Close</button>
<button type="submit" name="submit" class="btn btn-primary">Submit</button>
</div>
</div>
</form>
</div>
</div>

最佳答案

<?php 
.
.
echo "<td class='text-center'>
<ul class='icons-list'>
<a href='#modal_edit' class='modalButton' style='color:#000;' data-teacherid='{$row['username']}' data-toggle='modal' data-target='#modal_edit' data-popup='tooltip' title='Edit' data-container='body'>
<i class='icon-pencil5'></i>
</a>";

echo "<a href='delete.php?teacherid={$row['username']}' style='color:#000;'><i class='icon-cross2' data-popup='tooltip' title='Delete'
data-container='body'></i></a></ul></td>";
echo '</tr>';
++$c;
}
?>

将此代码放在某处,但在同一页面中。

<div id="modal_edit" class="modal fade" style="font-weight: normal;">
<div class="modal-dialog">
<div class="modal-content">

</div>
</div>
</div>

JS

<script>
$('.modalButton').click(function(){
var teacherid = $(this).attr('data-teacherid');
$.ajax({url:"ajax_modal_edit.php?teacherid="+teacherid,cache:false,success:function(result){
$(".modal-content").html(result);
}});
});
</script>

创建一个页面名称 ajax_modal_edit.php(如果您要更改此页面名称。也要更改 <script></script> 标签。两者都是相关的。)

ajax_modal_edit.php

<?php
$teacherid = $_GET['teacherid'];

// Use this `$teacherid` in query to get all required/appropriate field
?>

<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">&times;</button>
<h5 class="modal-title">Add Teacher <?php echo $teacherid;?></h5>
</div>
<form action="" method="POST">
<div class="modal-body">
<div class="form-group">
<div class="row">
<div class="col-sm-6">
<label>Full Name</label>
<input type="text" name="fullname" class="form-control" required>
</div>

<div class="col-sm-6">
<label>Mobile Number</label>
<input type="text" name="mobno" class="form-control" required>
</div>
</div>
</div>
<div class="form-group">
<div class="row">
<div class="col-sm-6">
<label>Password</label>
<input type="password" name="password" class="form-control" required>
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-link" data-dismiss="modal">Close</button>
<button type="submit" name="submit" class="btn btn-primary">Submit</button>
</div>
</div>
</form>

更多信息,查看Passing data via Modal Bootstrap and getting php variable?

关于php - 如何在模态中传递当前行值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35273035/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com