所以,这就是交易。我有一个 PHP 函数,它从 MySQL 表生成数据,并添加用于更改数据的链接以及从表/库中删除数据。
基本上,我想在删除表中的行之前调用确认模式。由于用于删除数据的 PHP 代码运行良好,我认为在删除数据之前进行某种排序确认会很好
因此,我尝试使用 this link 中的资源创建确认模式但没有任何运气。
这里是连接到 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>
函数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>';
}
这是我的 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);
}
最后,这是模态 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
}
我知道这是一个有点长的问题,但我想尽可能详细地阐述我的问题,因为我已经有这个问题几个月了,公平地说,PHP 代码运行得很好,我从来没有真正尝试过直到最近才做任何事情,因为我意识到我需要某种确认才能在未来的应用程序中删除数据。谢谢
最佳答案
所以,我终于明白了。使用 imran 发布的链接,我已成功在我的应用程序上复制并启动
在函数的一部分中,如果从此更改,将创建“删除”链接
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 '<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>';
接下来,在我的 js 文件中,我添加了这个
$('#confirm-delete').on('show.bs.modal', function(e) {
$(this).find('.btn-ok').attr('href', $(e.relatedTarget).data('href'));
});
最后在代码的连接部分(这就是我连接到 MySQL 的地方,try/catch block -->查看问题并使用 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>
这里,希望对你有帮助:)
关于javascript - 使用 PHP MySQL 确认 Bootstrap 模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37545301/