简体   繁体   中英

load bootstrap modal as alert after inserting data to database

im new to php and still learning. i tried to customized bootstrap template and tried to connect a form to a database.

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 :

<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

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)? 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 :

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

then stock.php on the head :

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

here is a demo only for the alert demo

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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