简体   繁体   English

将数据插入数据库后,加载引导模式作为警报

[英]load bootstrap modal as alert after inserting data to database

im new to php and still learning. 我是php新手,还在学习中。 i tried to customized bootstrap template and tried to connect a form to a database. 我尝试自定义引导程序模板,并尝试将表单连接到数据库。

insert.php insert.php

$con=mysqli_connect("localhost","root","","finalproject");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

$sql="INSERT INTO stokbarang (Merek, Tipe, Harga)
VALUES
('$_POST[merek]','$_POST[tipe]','$_POST[harga]')";

    if (!mysqli_query($con,$sql))
      {
      die('Error: ' . mysqli_error($con));
      }

header("Location: stock.php");

and a page where the form is stock.php, the form is below : 以及表单为stock.php的页面,表单如下:

<form name="sentMessage" class="well" id="contactForm" novalidate action = "insert.php" method = "post">
<legend>Masukkan Data</legend>
     <div class="control-group">
     <div class="controls">
                     <label>Merek Mobil</label>
               <input type="text" class="form-control" placeholder="Merek mobil" id="merek" required
                         data-validation-required-message="Masukkan Merek mobil" name = "merek"/>
                         <p class="help-block"></p>
     </div>
         </div>         
   <div class="control-group">
  <div class="controls">
     <label>Tipe Mobil</label>
     <input type="text" class="form-control" placeholder="Tipe Mobil" id="email" required
                             data-validation-required-message="Masukkan tipe mobil" name = "tipe"/>
                </div>
         </div>         
                <div class="control-group">
                  <div class="controls">
                   <label>Harga</label>
                        <input type="number" class="form-control" placeholder="Harga"
                            id="email" required
                            data-validation-required-message="Masukkan harga mobil" name = "harga"/>
                </div>      

        <br>
         <div id="success"> </span></div> <!-- For success/fail messages -->
         <br>
         <button type="submit" class="btn btn-primary pull-right">Insert</button><br/><br>
          </form>

the form is work and i can insert the data by clicking Insert button to database. 该表格是有效的,我可以通过单击“插入”按钮将数据插入数据库。 Now i want to add a modal as alert after the form is submitted to database in stock.php 现在,我想在表单提交到stock.php中的数据库后添加模式作为警报

i modified the Insert button as following 我修改了插入按钮如下

<button type="submit" class="btn btn-primary pull-right" data-toggle="modal" data-target="#myModal">Insert</button><br/><br>

here is the modal : 这是模态:

<!-- Modal -->
<div class="modal fade" id="myModal" 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">&times;</button>
        <h4 class="modal-title" id="myModalLabel">SUCCESS</h4>
      </div>
       <div class="modal-body">
        <p>Data Inserted!!</p>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>

      </div>
    </div><!-- /.modal-content -->
  </div><!-- /.modal-dialog -->
</div><!-- /.modal -->

but it seems like the button only trigger the modal to appear without submit the form to database. 但似乎按钮仅触发模式出现而无需将表单提交给数据库。 Any suggestion to make the modal appear after successful inserting data to database (after redirecting to stock.php)? 在成功将数据插入数据库后(重定向到stock.php之后),是否有任何使模式显示的建议? or maybe there is better way to make alert after redirect? 还是有更好的方法来使重定向后发出警报? thank you for your time and help :) 谢谢您的时间和帮助:)

change the header location on insert.php as below : 更改insert.php上的标头位置,如下所示:

header("Location: stock.php?sucsess=true");

then stock.php on the head : 然后stock.php

<script type="text/javascript">
<?php
if ($_GET['sucsess'] =='true'){
echo '$(function() {
$( "#myModal" ).dialog();
});'
}
?>
</script>

here is a demo only for the alert demo 这是仅用于警报演示的演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM