我是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");


以及表单为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>


该表格是有效的,我可以通过单击“插入”按钮将数据插入数据库。
现在我想在表单提交到stock.php中的数据库后添加一个模式作为警报

我修改了插入按钮如下

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


这是模态:

<!-- 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 -->


但似乎该按钮仅触发模式出现而无需将表单提交给数据库。在成功将数据插入数据库后(重定向到stock.php之后),是否有任何使模式显示的建议?还是有更好的方法来使重定向后发出警报?谢谢您的时间和帮助:)

最佳答案

更改insert.php上的标头位置,如下所示:

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


然后在头上stock.php

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


这是仅针对警报demo的演示

关于javascript - 将数据插入数据库后,将引导模式加载为警报,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20005965/

10-17 02:50