twitter-bootstrap 将当前的 $row 值传递给 Bootstrap Modal
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16265850/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Pass a current $row value to Bootstrap Modal
提问by Aaron St. Clair
Table:
桌子:
<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>
My goal is to pass the value of $row['ssn'] into my #delModal. I assume the problem lies within:
我的目标是将 $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>
It is my understanding that with the above syntax I should be able to use $_GET['ssn']inside the Bootstrap Modal to pull the value, but when I var_dump($ssn)the value only NULLis printed.
我的理解是,使用上述语法,我应该能够$_GET['ssn']在 Bootstrap Modal 中使用来拉取值,但是当我var_dump($ssn)只NULL打印值时。
Here is the Modal:
这是模态:
<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>
The overall purpose of this implementation is to use the Modal as a means to not accidentally delete an employee by prompting the user with a warning. I cannot just use $rowwithin the Modal as it will pull the last row every time, and I'm sure there's a better way of achieving what I'm trying to do than what I'm trying to do here, but I'm not sure what that way is.
此实现的总体目的是使用 Modal 作为一种手段,通过向用户提示警告来避免意外删除员工。我不能只$row在 Modal 中使用,因为它每次都会拉最后一行,而且我确信有比我在这里尝试做的更好的方法来实现我想要做的事情,但我不是确定那是什么方式。
采纳答案by BK004
Rather than passing value as get directly echo your values in model, and get an advantage of both being in same file.
而不是将值作为 get 直接在模型中回显您的值,并获得两者都在同一个文件中的优势。
call your model like this
像这样称呼你的模型
<a class="btn btn-danger" href="#delModal" data-toggle="modal"><i class="icon-trash icon-white"></i> Delete</a>
In model try like this
在模型中尝试这样
<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>
Hope this helps.
希望这可以帮助。
回答by Old Ceylon Mudliar
Hello I had the same issue and almost the same code as you are using. But I able to solve that issue later by passing my parameter value as href id.
您好,我遇到了同样的问题,并且与您使用的代码几乎相同。但是我稍后可以通过将我的参数值作为 href id 传递来解决该问题。
I had a button named READ, to open my model window. Here is the HTML code for my button.
我有一个名为 READ 的按钮,可以打开我的模型窗口。这是我的按钮的 HTML 代码。
<a button type="button" href="#<?php echo''.$u_id.'';?>" data-toggle="modal" class="btn btn-primary"> READ </a>
Here in that above code, you can see i have used id(column name and the primary key) of the message as the href id!
在上面的代码中,您可以看到我使用消息的 id(列名和主键)作为 href id!
And here is the my code for 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">×</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>
so once I passed the id (parameter) in to the modal, I can get other values using simple sql query.
因此,一旦我将 id(参数)传递给模态,我就可以使用简单的 sql 查询获取其他值。
So for your code also, i'm suggesting you to pass your primary key column value (Eg:id) as the button href id and div id.
因此,对于您的代码,我建议您将主键列值(例如:id)作为按钮 href id 和 div id 传递。
回答by jdkos
I was faced with a simular situation. My solution was:
我面临着一个类似的情况。我的解决方案是:
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'>×</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%'> </td>");}
}
}
}
}
回答by Shailender Ahuja
the best way is to use Jquery Ajax call like the following step: - 1. Create a function which get a parameter (or more). 2. Which is use to process Ajax post method. 3. And on return print HTML data in the modal div. 4. Call that function along with modal button "onClick" event...
最好的方法是像以下步骤一样使用 Jquery Ajax 调用: - 1. 创建一个获取参数(或更多)的函数。2. 用来处理Ajax post的方法。3. 并在模态 div 中返回打印 HTML 数据。4. 调用该函数以及模态按钮“onClick”事件...
And rest will be done easily the best and simple way to do that
休息将很容易完成,这是最好和最简单的方法

