Активация модального окна Bootstrap
Как сделать чтобы при перегрузке страницы по срабатыванию условия в PHP окно активировалось. Например в условии
if($_POST){
}
Ниже пример из руководства Bootstrap 4 с активацией окна по кнопке.
<!-- Button trigger modal -->
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal">
Запустить модальное окно
</button>
<!-- Modal -->
<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
...
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
</div>
Я вывожу таблицу с кнопкой перед каждой записью и хочу чтобы по нажатию кнопки открывалось модальное окно на редактирование. У меня не получается, наверно потому что атрибут type="submit" а для bootstrapa нужно "button".
<form action="" method="post"">
<table class="table-sm table-striped table-hover table-bordered">
<thead class="table-success sticky-top">
<?php
$zag = array (' ','Код','Тип','Описание');
echo "<tr>";
foreach($zag as $key => $value) {
echo '<th scope="col">'."$value"."</th>";
}
echo "</tr>";
?>
</thead>
<tbody>
<?php
for ($row_no = 0; $row_no <= $res->num_rows - 1; $row_no++) {
$res->data_seek($row_no);
$row = $res->fetch_assoc();
echo '<tr class=" m-0 p-0">';
echo '<td> <input type="radio" name="raz"/>'.$row[id].' </td>';
echo '<td> <button type="submit" class="btn btn-primary" name="but1" value="'.$row[id].'" >'.$row[id].'</button> </td>';
foreach($row as $key => $value) {
echo '<td>'." $value </td>";
}
echo "</tr>";
}
$res->free();
?>
</tbody>
</table>
</form>
Ответы (2 шт):
Немного криво, но должно работать. Если есть условие, просто выводим js скрипт, который нажимает на кнопку.
if($_POST){
echo '
<button type="button" id="buttonID" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal">
Запустить модальное окно
</button>
<script>
$("#buttonID").click();
</script>';
}
Вообще за активацию модального окна в Бутстрапе (в том числе после перезагрузки страницы) отвечает конструкция $('#myModal').modal('show'):
$('#myModal').modal('show')
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.4.1.slim.min.js" integrity="sha384-J6qa4849blE2+poT4WnyKhv5vZF5SrPo0iEjwBvKU7imGFAV0wwj1yYfoRSJoZ+n" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js" integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6" crossorigin="anonymous"></script>
<div class="modal" tabindex="-1" role="dialog" id="myModal">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Modal title</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<p>Modal body text goes here.</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
А вот что значит по срабатыванию условия в PHP - не очень ясно. Чтобы какой-то php-код отработал на сервере и что-то вернул клиенту, его надо как-то запустить - то есть отправить на сервер запрос. Чем, как и какой именно запрос Вы отправляете?