如何在模态中传递表的ID值

时间:2017-03-27 17:00:45

标签: php mysql

我想以模态形式传递student的id值。当我点击1stgrade按钮时,这是我的表格代码:

<tbody>
<?php
require_once '../dbconfig.php';

$stmt = $db_con->prepare("SELECT * FROM userinfo WHERE role='student' AND gradelvl='7' AND sectionname='$_POST[table7]' ORDER BY lrnno ASC");
$stmt->execute();
while($row=$stmt->fetch(PDO::FETCH_ASSOC))
{
?>
    <tr>
    <td><?php echo $row['fname']; ?></td>

这是我的模态形式:

<td>
    <div align="center">
        <form action="studentprofilegradeaction.php" method="POST" class="form-horizontal">
            <input type='text' name='id' value='<?php echo $row['id']; ?>' />
            <!-- Button HTML (to Trigger Modal) -->
            <a href="#1stgrade7"  class="btn btn-info" data-toggle="modal"><i class="fa fa-building"></i>1st</a>
            <!-- Modal HTML -->

            <div id="1stgrade7" class="modal fade">
                <div class="modal-dialog">
                    <div class="modal-content">
                        <div class="modal-header">
                            <input type='text' value='<?php echo $row['id'] ?>' />

                            <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                            <h2 class="modal-title">Please Fill Correctly</h2>
                        </div>
                        <div class="modal-header">
                            <h4 class="modal-title">1st Grading Result </h4>
                        </div>

                       <div class="modal-body">
                           <div align="left">
                                <b>Filipino:</b>
                                <input type="text" class="form-control" name="c71stgfilipino" >
                           </div>

我想将id号传递给modal 1stgrade。

1 个答案:

答案 0 :(得分:0)

替换您的锚标记:

<a href="#1stgrade7"  class="btn btn-info" data-toggle="modal"><i class="fa fa-building"></i>1st</a>

=&GT;

<a id="openModal" data-id="<?php echo $row['id']; ?>" class="btn btn-info"><i class="fa fa-building"></i>1st</a>

从while循环中删除其他不必要的代码,将模态代码保留在循环外部并为modal中的id输入提供id:

<input id="userId" type='text' value='<?php echo $row['id'] ?>' />

然后你需要jquery读取点击行的od并将其设置为modal后写:

<script>
$(document).ready(function() {

 $("#openModal").click(function() {

  //remove previous value
  $("#userId").val("");

  //this ll set id to your modal input
  $("#userId").val($(this).attr("data-id"));

  //open the modal
  $("#1stgrade7").modal("show");

 });
});
</script>