[英]Confirmation Bootstrap modal using PHP&MySQL
So, here is the deal. 所以这里是交易。 I have a PHP function which generates data from MySQL table and adds links for change of data and delte data from table/base.
我有一个PHP函数,该函数从MySQL表生成数据,并添加用于更改数据和表/库中数据的链接。
It generates something like this 它产生这样的东西
Basically, I want to call confirmation modal before deleting a row in a table. 基本上,我想在删除表中的一行之前调用确认模式。 As PHP code for deleting data works nicely I thought that it would be nice to have some sort confirmation before deleting data
由于用于删除数据的PHP代码很好用,所以我认为在删除数据之前进行一些排序确认会很好
So, I tried to create confirmation modal using resources from this link but without any luck. 因此,我尝试使用此链接中的资源创建确认模式,但没有任何运气。
Here is a PHP code that is connecting to the MySQL db 这是一个连接到MySQL数据库的PHP代码
$db = connectPDO();
$sql = 'SELECT * from drzava ORDER BY id ASC';
$podaci = $db->query($sql);
$nazivi_stupaca = array('ID', 'Oznaka','Naziv','Valuta');
showHTMLTableWithEditDeleteLink($nazivi_stupaca, $podaci, 'drzava');
closePDO($db);
} catch (PDOException $e) {
showPDOErrors($e, $db);
}
// ===============here is HTML CODE for modal===========
?>
<div id="confirmModal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h3 id="myModalLabel">Delete?</h3>
</div>
<div class="modal-body">
<p>Are you sure you wish to delete?</p>
</div>
<div class="modal-footer">
<button class="btn" data-dismiss="modal" aria-hidden="true">Cancel</button>
<button onclick="ok_hit()" class="btn btn-primary">OK</button>
</div>
</div>
Code for function showHTMLTableWithEditDeleteLink is here 函数showHTMLTableWithEditDeleteLink的代码在这里
function showHTMLTableWithEditDeleteLink($header_arr, $data_arr, $table_name) {
echo '<div class="table-responsive">';
echo '<table class="table table-hover table-striped table-bordered table-condensed">';
echo '<thead>';
echo '<tr>';
echo '<th>Rbr.</th>';
foreach($header_arr as $naziv_stupca) {
echo '<th th style="text-align: center;">', $naziv_stupca, '</th>';
}
echo '<th style="text-align: center" colspan=2>Akcija</th>';
echo '</tr>';
echo '</thead>';
echo '<tbody>';
$rbr = 1;
foreach($data_arr as $polja) {
echo "<tr>";
echo "<td >", $rbr++, ".</td>";
foreach ($polja as $pozicija => $vrijednost) {
if( is_integer($pozicija) ) {
echo "<td >", $vrijednost, "</td>";
}
}
echo "<td>";
if(myAuth::checkRights('UPDATE')){
echo '<a href="';
echo $table_name.'_promjena.php';
echo '?id=';
echo $polja[0];
echo '"class="btn btn-success btn-xs" role="button" ';
echo '"><span class="glyphicon glyphicon-pencil"></span> Change</a>';
}
echo "</td>";
echo "<td>";
if(myAuth::checkRights('DELETE')){
// link for delete
echo '<a class="btn btn-danger btn-xs"';
echo 'role="button" onclick="show_confirm()"';//calling modal before submiting
echo 'href="';
echo $table_name.'_brisanje.php';
echo '?id=';
echo $polja[0];
echo '"><span class="glyphicon glyphicon-trash"></span> Delete!</a>';
}
echo "</td>";
echo "</tr>";
}
echo '</tbody>';
echo '</table>';
echo '</div>';
}
And here is my PHP delete part of the code 这是我的PHP删除代码的一部分
try {
$sql = 'DELETE FROM drzava WHERE id = :id';
$db = connectPDO();
$stmt = $db->prepare($sql);
$stmt->bindParam(':id',$id);
$id = (int)$_GET['id'];
$stmt->execute();
echo "Pobrisana kava za id: $id<br>";
closePDO($db);
} catch (PDOException $e) {
showPDOErrors($e, $db, $stmt);
}
And finally, here is script.js for modal id 最后,这是用于模式ID的script.js
// function : show_confirm()
function show_confirm(){
// shows the modal on button press
$('#confirmModal').modal('show');
}
// function : ok_hit()
function ok_hit(){
// hides the modal
$('#confirmModal').modal('hide');
alert("OK Pressed");
// all of the functions to do with the ok button being pressed would go in here
}
I'know that it's a bit long question, but I wanted to elaborate my problem as detailed as possible, because I'm having this problem for a months know and to be fair PHP code worked nicely and I never really tried to do anything till recently, because I realized that I need some sort of confirmation for data removing in my future application. 我知道这是一个很长的问题,但是我想尽可能详细地阐述我的问题,因为我已经有几个月的时间知道这个问题了,并且公平地讲,PHP代码可以很好地工作,而且我从来没有真正尝试做任何事情,直到最近,因为我意识到我需要某种确认才能在将来的应用程序中删除数据。 Thank you
谢谢
So, I' ve finally figure it out. 所以,我终于弄清楚了。 Using link that imran posted I've managed to successfully replicate and initiate on my application
使用imran发布的链接,我已经成功地在应用程序上成功复制和初始化
In a part of the function where "delete" link is created if changed from this 在此功能的一部分中,如果对此进行更改,则会创建“删除”链接
echo '<a class="btn btn-danger btn-xs"';
echo 'role="button" onclick="show_confirm()"';//calling modal before submiting
echo 'href="';
echo $table_name.'_brisanje.php';
echo '?id=';
echo $polja[0];
echo '"><span class="glyphicon glyphicon-trash"></span> Delete!</a>';
To this 对此
echo '<a href="#" ';
echo 'data-href="';
echo $table_name.'_brisanje.php';
echo '?id=';
echo $polja[0].'" ';
echo 'data-toggle="modal"';
echo 'data-target="#confirm-delete "';
echo 'class="btn btn-danger btn-xs" role="button" ';
echo '"><span class="glyphicon glyphicon-trash"></span> Izbriši</a>';
Next, on my js file, I 've added this 接下来,在我的js文件中,我已经添加了
$('#confirm-delete').on('show.bs.modal', function(e) {
$(this).find('.btn-ok').attr('href', $(e.relatedTarget).data('href'));
});
And finally on my connecting part of the code (that's where I'm connecting to MySQL, try/catch block-->see question and using showHTMLTableWithEditDeleteLink function) I've added html for modal 最后,在代码的连接部分(这是我连接到MySQL的地方,尝试/捕获块->参见问题并使用showHTMLTableWithEditDeleteLink函数),我为模态添加了html
<div class="modal fade" id="confirm-delete" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title" id="myModalLabel">...some text...</h4>
</div>
<div class="modal-body">
<p><p class="debug-url"></p>
<p>...some text...</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">CANCEL</button>
<a class="btn btn-danger btn-ok">OK</a>
</div>
</div>
</div>
</div>
Here, hope it helps :) 在这里,希望对您有所帮助:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.