将当前$ row值传递给Bootstrap Modal

时间:2013-04-28 17:50:20

标签: php variables twitter-bootstrap modal-dialog

表:

<table class="table table-bordered">
        <thead>
            <tr>
                <th>Works At</th>
                <th>First Name</th>
                <th>Middle Initial</th>
                <th>Last Name</th>
                <th>Start Date</th>
                <th>SSN#</th>
                <th>Modify</th>
            </tr>
        </thead>
        <?php
            $query = "SELECT * FROM employees";

            $stmt = $db->prepare($query);
            $stmt->execute();

            foreach ($stmt as $row): ?>
                <tr>
                    <td><?php echo $row['rest_id_employees']; ?></td>
                    <td><?php echo $row['fname']; ?></td>
                    <td><?php echo $row['minit']; ?></td>
                    <td><?php echo $row['lname']; ?></td>
                    <td><?php echo $row['start_date']; ?></td>
                    <td><?php echo $row['ssn']; ?></td>
                    <td style="text-align: center">
                        <div class="btn-toolbar">
                            <div class="btn-group">
                                <a class="btn btn-danger" href="<?php echo $_SERVER['PHP_SELF'].'?ssn='.urlencode($row['ssn']); ?>#delModal" data-toggle="modal"><i class="icon-trash icon-white"></i> Delete</a>
                                <a class="btn btn-warning" href="#editModal"><i class="icon-wrench icon-white"></i> Edit</a>
                            </div>
                        </div>
                    </td>
                </tr>
        <?php endforeach; ?>
    </table>

我的目标是将$ row ['ssn']的值传递给我的#delModal。我认为问题在于:

<a class="btn btn-danger" href="<?php echo $_SERVER['PHP_SELF'].'?ssn='.urlencode($row['ssn']); ?>#delModal" data-toggle="modal"><i class="icon-trash icon-white"></i> Delete</a>

我的理解是,通过上面的语法,我应该可以在Bootstrap模式中使用$_GET['ssn']来提取值,但是当我var_dump($ssn)时,只打印NULL的值

以下是模态:

<div class="modal hide" id="delModal" aria-hidden="true">
        <div class="modal-header">
            <h3 style="color: red">WARNING</h3>
        </div>
        <div class="modal-body">
            <?php
                $ssn = $_GET['ssn'];
                var_dump($ssn);
            ?>
            <h4>You are about to delete --- from your Employee database.</h4>
            <p>An employee cannot be recoverd once they have been deleted. They can be added to the system again, but
                all data about the employee will be lost if you delete them. Are you sure you mean to delete this
                employee from the database?</p>
        </div>
        <div class="modal-footer">
            <button class="btn" data-dismiss="modal" aria-hidden="true">Cancel</button>
            <a class="btn btn-danger" href="deleteemployee.php?ssn=<?php echo urlencode($ssn); ?>"><i class="icon-trash icon-white"></i> Delete</a>
        </div>
    </div>

此实现的总体目的是使用Modal作为不通过提示用户警告而意外删除员工的方法。我不能在模态中使用$row,因为它每次都会拉最后一行,而且我确信有更好的方法来实现我想做的事情,而不是我在这里尝试做的事情,但我不确定那是什么方式。

4 个答案:

答案 0 :(得分:3)

您好我遇到了与您使用的相同的问题和几乎相同的代码。但我稍后可以通过将参数值传递为href id来解决该问题。

我有一个名为READ的按钮,用于打开我的模型窗口。这是我的按钮的HTML代码。

<a button type="button" href="#<?php echo''.$u_id.'';?>" data-toggle="modal" class="btn btn-primary"> READ  </a>

在上面的代码中,您可以看到我使用了消息的id(列名和主键)作为href id!

这是 MODEL WINDOW

的代码
<div class="modal" id="<?php echo''.$u_id.'';?>">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
          <h4 class="modal-title">Create public Event</h4>
      </div>
      <div class="modal-body">

    <?php echo '.$u_id.';?>
    </div>
  </div>
</div>

所以一旦我将id(参数)传递给模态,我可以使用简单的sql查询获取其他值。

因此,对于您的代码,我建议您将主键列值(例如:id)作为按钮href id和div id传递。

答案 1 :(得分:0)

最好的方法是使用Jquery Ajax调用 喜欢以下步骤: -  1.创建一个获取参数(或更多)的函数。  2.哪个用于处理Ajax post方法。  3.返回时在模态div中打印HTML数据。  4.将该功能与模态按钮一起调用&#34; onClick&#34;事件...

休息将以最简单的方式轻松完成

http://infotonicsmedia.com

答案 2 :(得分:0)

我遇到了类似的情况。 我的解决方案是:

private function pri_show_weekcontent($p_sCourse){ 

for($iWeekcounter=1;$iWeekcounter<=10;$iWeekcounter++){ 
// Fetch the nesseray data from the database       
    $aCellContent = $this->fetchAssignmentscontents($p_sCourse,$iWeekcounter);
// Create an unique ID for the modal, in my case course+weeknumber
    echo("<div id='".$p_sCourse.$iWeekcounter."' class='modal fade' role='dialog'>");
// Build the modal
    echo("<div class='modal-dialog'>");
        echo("<div class='modal-content'>");
          echo("<div class='modal-header'>");
            echo("<button type='button' class='close' data-dismiss='modal'>&times;</button>");
            echo("<h4 class='modal-title'>Toelichting opdracht</h4>");
          echo("</div>");
          echo("<div class='modal-body'>");
// show the content from the database
            echo("<p>".$aCellContent[0][6]."</p>");
          echo("</div>");
          echo("<div class='modal-footer'>");
            echo("<button type='button' class='btn btn-default' data-dismiss='modal'>Close</button>");
          echo("</div>");
        echo("</div>");
      echo("</div>");
    echo("</div>");

    if(!empty($aCellContent)){
        if($_SESSION['verifiedUser']=='true'){echo("<td align='center' width='8%' bgcolor='white'><a href='editassignment.php?rn=".$aCellContent[0][0]."'><span title='".$aCellContent[0][6]."'>".$aCellContent[0][5]."</span></a></td>"); }
// call the unique ID model with a click.           
        else{echo("<td align='center' width='8%' bgcolor='white'><a data-toggle='modal' data-target='#".$p_sCourse.$iWeekcounter."'>".$aCellContent[0][5]."</a></td>");}
    }else{
        if($_SESSION['verifiedUser']=='true'){echo("<td align='center' width='8%' bgcolor='lightgreen'><a href='addassignment2.php?cn=".$_SESSION['sClassname']."&oo=".$p_sCourse."&pn=".$_SESSION['iPeriodNumber']."&wn=".$iWeekcounter."'>Geen huiswerk</a></td>");}
        else{echo("<td align='center' width='8%'>&nbsp;</td>");}
    }        
}  

}

答案 3 :(得分:-2)

而不是传递值作为get直接回显模型中的值,并获得两者在同一文件中的优势。

像这样调用您的模型

<a class="btn btn-danger" href="#delModal" data-toggle="modal"><i class="icon-trash icon-white"></i> Delete</a>

在模型中尝试这样

<div class="modal hide" id="delModal" aria-hidden="true">
    <div class="modal-header">
        <h3 style="color: red">WARNING</h3>
    </div>
    <div class="modal-body">
            <?php echo $row['ssn']; ?>
        <h4>You are about to delete --- from your Employee database.</h4>
        <p>An employee cannot be recovered once they have been deleted. They can be added to the system again, but
            all data about the employee will be lost if you delete them. Are you sure you mean to delete this
            employee from the database?</p>
    </div>
    <div class="modal-footer">
        <button class="btn" data-dismiss="modal" aria-hidden="true">Cancel</button>
        <a class="btn btn-danger" href="deleteemployee.php?ssn=<?php echo urlencode($ssn); ?>"><i class="icon-trash icon-white"></i> Delete</a>
    </div>
  </div>

希望这有帮助。