Search
 
SCRIPT & CODE EXAMPLE
 

PHP

Insert Data using modal

<?php  
//index.php
$connect = mysqli_connect("localhost", "root", "", "testing");
$query = "SELECT * FROM employee ORDER BY id DESC";
$result = mysqli_query($connect, $query);
 ?>  
<!DOCTYPE html>  
<html>  
 <head>  
  <title>Webslesson Tutorial | Bootstrap Modal with Dynamic MySQL Data using Ajax & PHP</title>  
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>  
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />  
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>  
 </head>  
 <body>  
  <br /><br />  
  <div class="container" style="width:700px;">  
   <h3 align="center">Insert Data Through Bootstrap Modal by using Ajax PHP</h3>  
   <br />  
   <div class="table-responsive">
    <div align="right">
     <button type="button" name="age" id="age" data-toggle="modal" data-target="#add_data_Modal" class="btn btn-warning">Add</button>
    </div>
    <br />
    <div id="employee_table">
     <table class="table table-bordered">
      <tr>
       <th width="70%">Employee Name</th>  
       <th width="30%">View</th>
      </tr>
      <?php
      while($row = mysqli_fetch_array($result))
      {
      ?>
      <tr>
       <td><?php echo $row["name"]; ?></td>
       <td><input type="button" name="view" value="view" id="<?php echo $row["id"]; ?>" class="btn btn-info btn-xs view_data" /></td>
      </tr>
      <?php
      }
      ?>
     </table>
    </div>
   </div>  
  </div>
 </body>  
</html>  

<div id="add_data_Modal" class="modal fade">
 <div class="modal-dialog">
  <div class="modal-content">
   <div class="modal-header">
    <button type="button" class="close" data-dismiss="modal">&times;</button>
    <h4 class="modal-title">PHP Ajax Insert Data in MySQL By Using Bootstrap Modal</h4>
   </div>
   <div class="modal-body">
    <form method="post" id="insert_form">
     <label>Enter Employee Name</label>
     <input type="text" name="name" id="name" class="form-control" />
     <br />
     <label>Enter Employee Address</label>
     <textarea name="address" id="address" class="form-control"></textarea>
     <br />
     <label>Select Gender</label>
     <select name="gender" id="gender" class="form-control">
      <option value="Male">Male</option>  
      <option value="Female">Female</option>
     </select>
     <br />  
     <label>Enter Designation</label>
     <input type="text" name="designation" id="designation" class="form-control" />
     <br />  
     <label>Enter Age</label>
     <input type="text" name="age" id="age" class="form-control" />
     <br />
     <input type="submit" name="insert" id="insert" value="Insert" class="btn btn-success" />

    </form>
   </div>
   <div class="modal-footer">
    <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
   </div>
  </div>
 </div>
</div>

<div id="dataModal" class="modal fade">
 <div class="modal-dialog">
  <div class="modal-content">
   <div class="modal-header">
    <button type="button" class="close" data-dismiss="modal">&times;</button>
    <h4 class="modal-title">Employee Details</h4>
   </div>
   <div class="modal-body" id="employee_detail">
    
   </div>
   <div class="modal-footer">
    <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
   </div>
  </div>
 </div>
</div>

<script>  
$(document).ready(function(){
 $('#insert_form').on("submit", function(event){  
  event.preventDefault();  
  if($('#name').val() == "")  
  {  
   alert("Name is required");  
  }  
  else if($('#address').val() == '')  
  {  
   alert("Address is required");  
  }  
  else if($('#designation').val() == '')
  {  
   alert("Designation is required");  
  }
   
  else  
  {  
   $.ajax({  
    url:"insert.php",  
    method:"POST",  
    data:$('#insert_form').serialize(),  
    beforeSend:function(){  
     $('#insert').val("Inserting");  
    },  
    success:function(data){  
     $('#insert_form')[0].reset();  
     $('#add_data_Modal').modal('hide');  
     $('#employee_table').html(data);  
    }  
   });  
  }  
 });




 $(document).on('click', '.view_data', function(){
  //$('#dataModal').modal();
  var employee_id = $(this).attr("id");
  $.ajax({
   url:"select.php",
   method:"POST",
   data:{employee_id:employee_id},
   success:function(data){
    $('#employee_detail').html(data);
    $('#dataModal').modal('show');
   }
  });
 });
});  
 </script>
Comment

How to load data to modal

<!DOCTYPE html>
<html lang="en">

<head>

    <meta charset="utf-8">

    <meta http-equiv="X-UA-Compatible" content="IE=edge">

    <meta name="viewport" content="width=device-width, initial-scale=1">

    <meta name="description" content="">

    <meta name="author" content="">

<title>Passing Value to Modal using jQuery</title>

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>

 <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />

 <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>


</head>

<body>

<div class="container">

 <div style="height:50px;"></div>

 <div class="well">

 <center><h2>Passing Values to Modal using jQuery</h2></center>

 <div style="height:10px;"></div>

 <table width="100%" class="table table-striped table-bordered table-hover">

  <thead>

   <th>Firstname</th>

   <th>Lastname</th>

   <th>Address</th>

   <th>Action</th>

  </thead>

  <tbody>

  <?php

   $conn = mysqli_connect("localhost","root","","pass_model");

   $query=mysqli_query($conn,"select * from `user`");

   while($row=mysqli_fetch_array($query)){

    ?>

    <tr>

    <td><span id="firstname"><?php echo $row['firstname']; ?></span></td>

    <td><span id="lastname"><?php echo $row['lastname']; ?></span></td>

    <td><span id="address"><?php echo $row['address']; ?></span></td>

    <td><button type="button" class="btn btn-success edit" value="<?php echo $row['userid']; ?>"><span class="glyphicon glyphicon-edit"></span> Edit</button></td>

    </tr>

    <?php

   }

  ?>  

  </tbody>

 </table>

 </div>

 <?php include('modal.php');?>

 <script type="text/javascript">

  $(document).ready(function(){

   $(document).on('click', '.edit', function(){

    var first= $('#firstname').text();// get firstname

    var last= $('#lastname').text(); // get lastname

    var address= $('#address').text(); //get address
    $('#edit').modal('show');//load modal


    $('#efirstname').val(first);

    $('#elastname').val(last);

    $('#eaddress').val(address);

   });

  });

 </script>

</body>



</html>
Comment

PREVIOUS NEXT
Code Example
Php :: laravel - How to concatenate URL and retrieve images from database in json format 
Php :: contact form dropdown from post 
Php :: provenienza geografica di un utente php webmaster 
Php :: How to display limited post content in WordPress 
Php :: twiml gather php 
Php :: Laravel validation rule for one item which can be email or phone numbe 
Php :: wpmu assign user to blog 
Php :: how do istart writing a php code 
Php :: php variable array for json encode data 
Php :: laravel auditing tray publishing 
Php :: refresh database tables yii 1 
Php :: public function __sleep() and __wakeup() 
Php :: php mysql foreach is bad pardics ? 
Php :: laravel create multiple rows 
Php :: how to upload images to sql database php PDO 
Php :: laravel find query 
Php :: prestashop get product id 
Php :: create custom rule in laravel 
Php :: simple php round example 
Php :: strings functions php 
Php :: laravel transaction 
Php :: User::factory()-create( 
Php :: php login system 
Java :: print hello world in java 
Java :: spring enable debug log level 
Java :: bootstrap center text vertically 
Java :: how to clear the screen by pressing a key in java 
Java :: return boolean value from stream 
Java :: stringjoiner stream java 
Java :: spring boot id auto generated 
ADD CONTENT
Topic
Content
Source link
Name
1+1 =