I'm trying to make a modal popup display allowing you to log in but only if you are not logged in.
I have tried many solutions but so far can only get the login to display in a new page.
Please see my code below
index.php
<?php
require 'database_connection.php';
if (!isset($_SESSION["type"])) {
echo "<script>$('#loginModal').modal('show');</script>";
}
require 'includes/header.php';
?>
***
***
if session type is
set code goes here
***
***
<div id="loginModal" class="modal fade">
<div class="modal-dialog">
<form method="post" id="product_form">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title"><i class="fa fa-plus"></i> Add Product</h4>
<button type="button" class="close" data-dismiss="modal">×</button>
</div>
<div class="modal-body">
<div class="form-group">
<label>Stock Code</label>
<input type="text" name="product_name" id="product_name" class="form-control" required disabled />
</div>
<div class="form-group">
<label name="category_label" id="category_label">Category</label>
<select name="category_id" id="category_id" class="form-control" required>
<option value="">Select Category</option>
<?php echo fill_category_list($connect);?>
</select>
</div>
<div class="form-group">
<label>Enter Product Quantity</label>
<div class="input-group">
<input type="text" name="product_quantity" id="product_quantity" class="form-control" required pattern="[+-]?([0-9]*[.])?[0-9]+" placeholder="Enter Qty" />
<span class="input-group-addon">
<select class="form-control" name="product_unit" id="product_unit" required disabled>
<option value="Box">Boxes</option>
</select>
</span>
</div>
</div>
<div class="form-group">
<label>Location</label>
<input type="text" name="product_location" id="product_location" class="form-control" required disabled />
</div>
</div>
<div class="modal-footer">
<input type="hidden" name="product_id" id="product_id" />
<input type="hidden" name="btn_action" id="btn_action" />
<input type="submit" name="action" id="action" class="btn btn-success" value="Add" />
</div>
</div>
</form>
</div>
</div>
database_connection.php
<?php
$connect = new PDO('mysql:host=localhost;dbname=continental3', 'root', 'password');
session_start();
?>
What am I missing?
On a side note if there is an easier or better way of doing this I'm happy to change my approach
Any help would be greatly appreciated
** UPDATE**
<?php
require 'database_connection.php'; //checks session type is set or not
require 'includes/header.php'; // Loads css and js files
if (!isset($_SESSION["type"])) { //session check
echo '<script>$(document).ready(function(){$("#loginModal").modal("show");});</script>';
} //open modal
require 'function.php';
require 'includes/navbar.php';
?>
The above code shows the modal as expected but only after the page has loaded. removing the document ready function the modal does not load but the rest of the page loads and displays (presumably because I do not use the else condition. When using the else condition as seen below nothing loads.
<?php
require 'database_connection.php';
require 'includes/header.php';
if (!isset($_SESSION["type"])) { ?>
echo '<script>$(document).ready(function(){$("#loginModal").modal("show");});</script>';
<?php } else {
require 'function.php';
require 'includes/navbar.php';
?>
***
***
if session type is
set code goes here
***
***
<div id="loginModal" class="modal fade">
<div class="modal-dialog">
<form method="post" id="product_form">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title"><i class="fa fa-plus"></i> Add Product</h4>
<button type="button" class="close" data-dismiss="modal">×</button>
</div>
<div class="modal-body">
<div class="form-group">
<label>Stock Code</label>
<input type="text" name="product_name" id="product_name" class="form-control" required disabled />
</div>
<div class="form-group">
<label name="category_label" id="category_label">Category</label>
<select name="category_id" id="category_id" class="form-control" required>
<option value="">Select Category</option>
<?php echo fill_category_list($connect);?>
</select>
</div>
<div class="form-group">
<label>Enter Product Quantity</label>
<div class="input-group">
<input type="text" name="product_quantity" id="product_quantity" class="form-control" required pattern="[+-]?([0-9]*[.])?[0-9]+" placeholder="Enter Qty" />
<span class="input-group-addon">
<select class="form-control" name="product_unit" id="product_unit" required disabled>
<option value="Box">Boxes</option>
</select>
</span>
</div>
</div>
<div class="form-group">
<label>Location</label>
<input type="text" name="product_location" id="product_location" class="form-control" required disabled />
</div>
</div>
<div class="modal-footer">
<input type="hidden" name="product_id" id="product_id" />
<input type="hidden" name="btn_action" id="btn_action" />
<input type="submit" name="action" id="action" class="btn btn-success" value="Add" />
</div>
</div>
</form>
</div>
</div>
<?php } #close else ?>
Where did you set $_SESSION['type'] ???
Please try this
index.php
<?php
require 'database_connection.php';
if (!isset($_SESSION["type"])) { ?>
<script> $('#loginModal').modal('show'); </script>";
<?php } else {
require 'includes/header.php';
?>
***
***
if session type is
set code goes here
***
***
<div id="loginModal" class="modal fade">
<div class="modal-dialog">
<form method="post" id="product_form">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title"><i class="fa fa-plus"></i> Add Product</h4>
<button type="button" class="close" data-dismiss="modal">×</button>
</div>
<div class="modal-body">
<div class="form-group">
<label>Stock Code</label>
<input type="text" name="product_name" id="product_name" class="form-control" required disabled />
</div>
<div class="form-group">
<label name="category_label" id="category_label">Category</label>
<select name="category_id" id="category_id" class="form-control" required>
<option value="">Select Category</option>
<?php echo fill_category_list($connect);?>
</select>
</div>
<div class="form-group">
<label>Enter Product Quantity</label>
<div class="input-group">
<input type="text" name="product_quantity" id="product_quantity" class="form-control" required pattern="[+-]?([0-9]*[.])?[0-9]+" placeholder="Enter Qty" />
<span class="input-group-addon">
<select class="form-control" name="product_unit" id="product_unit" required disabled>
<option value="Box">Boxes</option>
</select>
</span>
</div>
</div>
<div class="form-group">
<label>Location</label>
<input type="text" name="product_location" id="product_location" class="form-control" required disabled />
</div>
</div>
<div class="modal-footer">
<input type="hidden" name="product_id" id="product_id" />
<input type="hidden" name="btn_action" id="btn_action" />
<input type="submit" name="action" id="action" class="btn btn-success" value="Add" />
</div>
</div>
</form>
</div>
</div>
<?php } #close else ?>
database_connection.php
<?php
$connect = new PDO('mysql:host=localhost;dbname=continental3', 'root', 'password');
session_start();
?>
Related
I have created an Edit Item popup Bootstrap modal on home page. Now what I want is to update the selected item details using the PHP.
Here i am passing item id through the URL in order to select the item
Now when I press edit Button it opens a Popup with all the details in it( already filled) but when I try to click on Save Changes Button it just do nothing and My data not updated. I have also used POST method but its not working.
Here is the Button that opens the Popup Modal
<?php
$res= mysqli_query($db,"SELECT* FROM `books`;");
if(mysqli_num_rows($res)>0){
while($row= mysqli_fetch_assoc($res)){
if($row!=0)
{
$bid= $row['book_id'];
?>
<ul>
<li>
<a onclick="$('#editModal<?php echo $row['book_id']?>').modal('show');" class="btn-show-modal edit-btn" data-toggle="modal"><i style="padding-right: 140px;" class="fas fa-edit"></i></a>
</li>
<!--Here is the Popup Modal for Edit -->
<div id="editModal<?php echo $row['book_id']?>" class="modal fade" tabindex="-1" role="dialog">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Edit This Book</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<form action="updatebook.php" method="POST">
<div class="form-group">
<label for="isbn" class="col-form-label">ISBN</label>
<input type="text" class="form-control" name="isbn" value="<?php echo $row['isbn'];?>">
</div>
<div class="form-group">
<label for="title" class="col-form-label">Enter Book Title</label>
<input type="text" class="form-control" name="title" value="<?php echo $row['title'];?>">
</div>
<div class="form-group">
<label for="author" class="col-form-label">Enter Author Name</label>
<input type="text" class="form-control" name="author" value="<?php echo $row['author'];?>">
</div>
<div class="form-group">
<label for="image" class="col-form-label">Image URL</label>
<input type="text" class="form-control" name="image" value="<?php echo $row['image'];?>">
</div>
<div class="form-group">
<label for="description" class="col-form-label">Enter Book Description</label>
<textarea class="form-control" name="description" value="<?php echo $row['description'];?>"></textarea>
</div>
<div class="form-group">
<label for="url" class="col-form-label">Book URL</label>
<input type="text" class="form-control" name="url" value="<?php echo $row['url'];?>"></textarea>
</div>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary" name="save">Save Changes</button>
</div>
</div>
</div>
</div>
<?php
}
}
}
else
{
echo"<h1 style='color: white;font-size:58px; font-family: Poppins; font-weight: 600;' class=m-2>U!Oh Nothing Found Here!</h1>
<button onclick=\"window.location.href='add_books_form.php'\" class=\"btn btn-info bg-primary m-5\">Contribute</button>";
}
?>
</ul>
Here is the updatebook.php Page
<?php
include "connection.inc.php";
if(isset($_POST['save']))
{
$id= $_POST['id'];
$isbn= $_POST['isbn'];
$title= $_POST['title'];
$author= $_POST['author'];
$image= $_POST['image'];
$desc= $_POST['description'];
$url= $_POST['url'];
$res = mysqli_query($db, "UPDATE books SET isbn= '$isbn', title= '$title', author= '$author',image= '$image', description= '$desc', url= '$url' WHERE book_id= '$id'");
?>
if($res)
{
<script type="text/javascript">
alert("Data Updated Successfully");
window.location.href= "home.php";
</script>
}
else
{
<script>
alert("Not Updated!");
window.location.href="home.php";
</script>
}
<?php
}
?>
Please Help me to solve this Update Issue i am facing for a long time. I have researched all through but didn't got any solution.
Change your Save button type to "submit" and remove the <a> hyperlink.
Use <form> as a part of both modal-body and modal-footer so the submit button will be part of it.
Take book_id in a hidden field so it will be the part of POST.
Look at the modal code below:
<div id="editModal<?php echo $row['book_id']?>" class="modal fade" tabindex="-1" role="dialog">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Edit This Book</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<form action="updatebook.php" method="POST">
<input type="hidden" name="id" value="<?php echo $row['book_id'];?>">
<div class="modal-body">
<div class="form-group">
<label for="isbn" class="col-form-label">ISBN</label>
<input type="text" class="form-control" name="isbn" value="<?php echo $row['isbn'];?>">
</div>
<div class="form-group">
<label for="title" class="col-form-label">Enter Book Title</label>
<input type="text" class="form-control" name="title" value="<?php echo $row['title'];?>">
</div>
<div class="form-group">
<label for="author" class="col-form-label">Enter Author Name</label>
<input type="text" class="form-control" name="author" value="<?php echo $row['author'];?>">
</div>
<div class="form-group">
<label for="image" class="col-form-label">Image URL</label>
<input type="text" class="form-control" name="image" value="<?php echo $row['image'];?>">
</div>
<div class="form-group">
<label for="description" class="col-form-label">Enter Book Description</label>
<textarea class="form-control" name="description" value="<?php echo $row['description'];?>"></textarea>
</div>
<div class="form-group">
<label for="url" class="col-form-label">Book URL</label>
<input type="text" class="form-control" name="url" value="<?php echo $row['url'];?>"></textarea>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary" name="save">Save Changes</button>
</div>
</form>
</div>
</div>
</div>
There is problem in opening bootstrap modal in iphones.
If i use static modal like below,
<a href="#responsive" data-toggle="modal"><button class="btn btn-green ">Add Product<i class="fa fa-plus"></i></button>
</a>
<!-- start: BOOTSTRAP EXTENDED MODALS -->
<div id="responsive" class="modal extended-modal fade no-display">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">
×
</button>
<h4 class="modal-title" style="color:red">Add New Product</h4>
</div>
<div class="modal-body">
<div class="row">
<form name="new" action="<?php echo base_url() ?>admin/addProduct" method="post" enctype="multipart/form-data" autocomplete="off">
<div class="col-md-6">
<h4><b>Category Name <span class="symbol required"></span></b></h4>
<p>
<select class="form-control" name="cat_id" required>
<option value="">---- Select Category ----</option>
<?php foreach($cat['list'] as $rows=>$value){ ?>
<option value="<?php echo $cat['list'][$rows]->cat_id; ?>"><?php echo ucwords($cat['list'][$rows]->cat_name); ?></option>
<?php } ?>
</select>
</p>
</div>
<div class="col-md-6">
<h4><b>Product Name <span class="symbol required"></span></b></h4>
<p>
<input class="form-control" type="text" name="item_name" required>
</p>
</div>
<div class="col-md-6">
<h4><b>Small Description <span class="symbol required"></span></b></h4>
<p>
<input class="form-control" type="text" name="small_desc">
</p>
</div>
<div class="col-md-6">
<h4><b>Quantity<span class="symbol required"></span></b></h4>
<p>
<input class="form-control" type="number" name="quantity" required min="1">
</p>
</div>
<div class="col-md-6">
<h4><b>Customer Price<span class="symbol required"></span></b></h4>
<p>
<input class="form-control" type="number" name="price" required min="1">
</p>
</div>
<div class="col-md-6">
<h4><b>Reseller Price<span class="symbol required"></span></b></h4>
<p>
<input class="form-control" type="number" name="reseller" required min="1">
</p>
</div>
<div class="col-md-6">
<h4><b>Color<span class="symbol "></span></b></h4>
<p>
<input class="form-control" type="text" name="color">
</p>
</div>
<div class="col-md-6">
<h4><b>Availability <span class="symbol required"></span></b></h4>
<select class="form-control" name="availability">
<option value="1">Available</option>
<option value="0">Not Available</option>
</select>
</div>
<div class="col-md-6">
<h4><b>Image <span class="symbol required"></span></b></h4>
<p>
<input class="form-control" accept="image/*" onchange="loadFile(event)" type="file" name="userfile" required multiple>
<img id="output" width="100px" height="100px" />
</p>
</div>
</div>
</div>
<div class="modal-footer">
<button type="reset" data-dismiss="modal" class="btn btn-light-grey">
Close
</button>
<button type="submit" class="btn btn-blue">
Add Product
</button>
</div>
</form>
</div>
</div>
</div>
<!-- End Modal -->
the modal will open properly and i can enter values.
But if use the modal for editing like below,
<a href="#edit<?php echo $product['list'][$row]->item_id; ?>" data-toggle="modal">
<button class="btn btn-blue"><i class="fa fa-pencil"></i> Edit </button>
</a>
<div id="edit<?php echo $product['list'][$row]->item_id?>" class="modal extended-modal fade no-display">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">
×
</button>
<h4 class="modal-title" style="color:red">Edit Product</h4>
</div>
<div class="modal-body">
<div class="row">
<form action="<?php echo base_url() ?>admin/editProduct" enctype="multipart/form-data" method="post" autocomplete="off">
<div class="col-md-6">
<h4><b>Category Name <span class="symbol required"></span></b></h4>
<p>
<select class="form-control" name="cat_id" required>
<option value="">---- Select Category ----</option>
<?php
foreach($cat['list'] as $rows=>$value){ ?>
<option value="<?php echo $cat['list'][$rows]->cat_id; ?>"<?php if($cat['list'][$rows]->cat_id == $product['list'][$row]->cat_id) echo "selected=selected"; ?>><?php echo ucwords($cat['list'][$rows]->cat_name); ?></option>
<?php } ?>
</select>
</p>
</div>
<div class="col-md-6">
<input type="hidden" name="item_id" value="<?php echo $product['list'][$row]->item_id;?>">
<input type="hidden" name="image_name" value="<?php echo $product['list'][$row]->image;?>">
<h4><b>Product Name <span class="symbol required"></span></b></h4>
<p>
<input class="form-control" type="text" name="item_name" value="<?php echo $product['list'][$row]->item_name;?>" required>
</p>
</div>
<div class="col-md-6">
<h4><b>Small Description <span class="symbol required"></span></b></h4>
<p>
<input class="form-control" type="text" name="small_desc" value="<?php echo $product['list'][$row]->small_desc;?>">
</p>
</div>
<div class="col-md-6">
<h4><b>Quantity<span class="symbol required"></span></b></h4>
<p>
<input class="form-control" type="number" name="quantity" value="<?php echo $product['list'][$row]->quantity;?>" min="1" required>
</p>
</div>
<div class="col-md-6">
<h4><b>Customer Price<span class="symbol required"></span></b></h4>
<p>
<input class="form-control" type="text" name="price" value="<?php echo $product['list'][$row]->price;?>" min="1" required>
</p>
</div>
<div class="col-md-6">
<h4><b>Reseller Price<span class="symbol required"></span></b></h4>
<p>
<input class="form-control" type="text" name="reseller" value="<?php echo $product['list'][$row]->reseller;?>" min="1" required>
</p>
</div>
<div class="col-md-6">
<h4><b>Color<span class="symbol"></span></b></h4>
<p>
<input class="form-control" type="text" name="color" value="<?php echo $product['list'][$row]->color;?>">
</p>
</div>
<div class="col-md-6">
<h4><b>Availability <span class="symbol required"></span></b></h4>
<select class="form-control" name="availability">
<?php if($product['list'][$row]->availability==1){;?>
<option value="1" selected>Available</option>
<option value="0">Not Available</option>
<?php } else {?>
<option value="1">Available</option>
<option value="0" selected>Not Available</option>
<?php }?>
</select>
</div>
<div class="col-md-6">
<h4><b>Status <span class="symbol required"></span></b></h4>
<select class="form-control" name="status">
<?php if($product['list'][$row]->status==0){;?>
<option value="0" selected>Inactive</option>
<option value="1">Active</option>
<?php } else {?>
<option value="0">Inactive</option>
<option value="1" selected>Active</option>
<?php }?>
</select>
</div>
<div class="col-md-6">
<h4><b>Image <span class=""></span>
</b></h4>
<input class="form-control" type="file" name="userfile">
<img src="<?php echo base_url()." assets/uploads/ ".$product['list'][$row]->image;?>" height="50px" width="50px" border="1px solid #FFF">
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" data-dismiss="modal" class="btn btn-light-grey">
Close
</button>
<button type="submit" class="btn btn-blue">
Update
</button>
</div>
</form>
</div>
</div>
</div>
With dynamic id, the modal will open with light black overlay on it. I can't edit the fields. Nothing is working inside the modal.
I am Not Understanding What is the problem with dynamic modal.
If Anyone knows Please reply.
You can try two things. 1. Move your form tag out and place it like this -
<form action="./admin/editProduct" enctype="multipart/form-data" method="post" autocomplete="off">
<div class="modal-body">
Give your modal z-index of 999 or so using css like this -
<style>
.modal {
z-index: 999;
}
</style>
`<div class="tab-content">
<div class="row">
<div class="col-lg-6">
<form method="post" data-toggle="modal" enctype="multipart/form-data" action="data/pegawai/proses.php">
<div class="form-group">
<label>NIP</label>
<input class="form-control" name="nip" type="text" value="<?php echo $r['nip'];?>" readonly="">
</div>
<div class="form-group">
<label>Nama Lengkap</label>
<input class="form-control" type="text" value="<?php echo $r['nm_pegawai'];?>" name="nama" placeholder="Nama Lengkap" autofocus="" required="">
</div>
<div class="form-group">
<label>Jenis Kelamin</label>
<div class="radio">
<label class="radio-inline">
<input type="radio" name="jk" id="optionsRadiosInline1" value="Laki-laki" <?php $ob->cek("Laki-laki",$r['jenis_kelamin'],"radio") ?>>Laki-laki
</label>
<label class="radio-inline">
<input type="radio" name="jk" id="optionsRadiosInline2" value="perempuan" <?php $ob->cek("Perempuan",$r['jenis_kelamin'],"radio") ?>>perempuan
</label>
</div>
</div>
<div class="form-group">
<label>Tempat Lahir</label>
<input class="form-control" type="text" value="<?php echo $r['tempat_lahir'];?>" name="tmp_lahir" placeholder="Tempat Lahir" required="">
</div>
<div class="form-group">
<label>Tanggal Lahir</label>
<input type="date" name="tgl_lahir" value="<?php echo $r['tanggal_lahir'];?>" class="form-control" placeholder="Tanggal Lahir" required="">
</div>
<div class="form-group">
<label>Agama</label>
<select class="form-control" name="agama" required="">
<option <?php $ob->cek("Islam",$r['agama'],"select") ?> value="Islam">Islam</option>
<option <?php $ob->cek("Kristen",$r['agama'],"select") ?> value="Kristen">Kristen</option>
<option <?php $ob->cek("Katholik",$r['agama'],"select") ?> value="Katholik">Katholik</option>
<option <?php $ob->cek("Protestan",$r['agama'],"select") ?> value="Protestan">Protestan</option>
<option <?php $ob->cek("Hindu",$r['agama'],"select") ?> value="Hindu">Hindu</option>
<option <?php $ob->cek("Budha",$r['agama'],"select") ?> value="Budha">Budha</option>
</select>
</div>
<div class="form-group">
<label>Alamat</label>
<textarea class="form-control" name="alamat" rows="3" placeholder="Alamat Lengkap" required=""><?php echo $r['alamat'];?></textarea>
</div>
</div>
<div class="col-lg-6">
<div class="form-group">
<label>Nomor Telepon</label>
<input class="form-control" type="text" value="<?php echo $r['no_telp'];?>" name="telp" onkeypress="return isNumberKey(event)" maxlength="15" placeholder="Nomor telepon" required="">
</div>
<div class="form-group">
<label>Tahun Masuk</label>
<input class="form-control" type="text" value="<?php echo $r['tahun_masuk'];?>" name="thn_masuk" placeholder="Tahun Masuk" onkeypress="return isNumberKey(event)" maxlength="4" required="">
</div>
<div class="form-group">
<label>Bagian</label>
<select class="form-control" name="bagian" required="">
<option <?php $ob->cek("Teller",$r['bagian'],"select") ?> value="Teller">Teller</option>
<option <?php $ob->cek("CSO",$r['bagian'],"select") ?> value="CSO">CSO</option>
<option <?php $ob->cek("BO",$r['bagian'],"select") ?> value="BO">BO</option>
</select>
</div>
<div class="form-group">
<label>Cabang</label>
<?php
echo '<select class="form-control" name="cabang" required="">';
$query=mysql_query("
SELECT *
FROM bank");
while($entry1=mysql_fetch_array($query))
{
echo "<option";
if($entry1['kd_bank']==$r['kd_bank']){echo " selected=selected";}
echo " value='".$entry1['kd_bank']."'>" . $entry1['nm_bank'] . "</option>";
}
echo "</select>";
?>
</div>
<div class="form-group">
<label>Kata Sandi</label>
<input class="form-control" type="password" value="<?php echo $r['password']?>" name="password" placeholder="Kata Sandi">
</div>
</div>
<!-- /.col-lg-6 (nested) -->
</div>
<!-- /.row (nested) -->
<div class="modal-footer">
<button type="button" name="ubah" class="btn btn-primary">Ubah</button>
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</form>
</div> `
my source code, my button cannot active, how to solving it?
Thank you
Pandhu
Make your button type as "submit" like below.
<button type="submit" name="ubah" class="btn btn-primary">Ubah</button>
Else you can also keep it as button, but in that case u need to use ajax(javascript) and onclick event on button , for example :
<button type="button" name="ubah" class="btn btn-primary" onclick="save()">Ubah</button>
where save() is some function in javascript that you will create to save the form data.
I have a modal (Bootstrap) for creating a new product. When I click the "close" button, which creates a new product, the old data still does not clear from the modal.
My code:
<div class="modal fade" id="product" role="dialog" aria-hidden="true" data-target="#myModal" data-backdrop="static" data-keyboard="false" >
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Create new product</h4>
</div>
<div class="modal-body">
<form role="form" action="{{ route('admin.product.addProduct')}}" method="post" id="frmProduct">
{{ csrf_field() }}
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label for="">Product Name</label>
<input type="text" class="form-control" id="" name="product_name">
</div>
<div class="form-group">
<label for="">Product Type</label>
<input type="text" class="form-control" id="" name="product_type_id">
</div>
<div class="form-group">
<label for="">Price</label>
<input type="text" class="form-control" id="" name="price">
</div>
<div class="form-group">
<label for="">Status</label>
<select class="form-control input-sm m-bot15" id="" name="status">
<option value="0">Inactive</option>
<option value="1">Active</option>
</select>
</div>
<div class="form-group">
<label for="img">Image</label>
<div class="input-group">
<span class="input-group-btn">
<span class="btn btn-default btn-file">
Choose <input type="file" id="imgInp">
</span>
</span>
<input type="text" class="form-control" name="product_image" readonly>
</div>
<img id='img-upload' class="image_responsive" />
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label for="">Description</label>
<textarea class="form-control" id="" name="description"></textarea>
</div>
<div class="form-group">
<label for="">Note</label>
<textarea class="form-control" id="" name="addition_information"></textarea>
</div>
</div>
</div>
</div>
<div class="modal-footer">
<input type="submit" value="Save" class="btn btn-success">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</form>
</div>
</div>
</div>
How can I clear the data when closing the modal?
You can add event listener to your close button by adding an id to your close button for example id=close-btn:
document.getElementById("close-btn").addEventListener("click", function(){
document.getElementById("frmProduct").reset();
});
You can use hidden.bs.modal event:
This event is fired when the modal has finished being hidden from the user (will wait for CSS transitions to complete).
The snippet:
$('#product').on('hidden.bs.modal', function(e) {
$(this).find('form').trigger('reset');
})
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://code.jquery.com/jquery-2.1.1.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<!-- Button trigger modal -->
<button type="button" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#product">
Launch modal
</button>
<div class="modal fade" id="product" role="dialog" aria-hidden="true" data-target="#myModal" data-backdrop="static" data-keyboard="false" >
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Create new product</h4>
</div>
<div class="modal-body">
<form role="form" action="{{ route('admin.product.addProduct')}}" method="post" id="frmProduct">
{{ csrf_field() }}
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label for="">Product Name</label>
<input type="text" class="form-control" id="" name="product_name">
</div>
<div class="form-group">
<label for="">Product Type</label>
<input type="text" class="form-control" id="" name="product_type_id">
</div>
<div class="form-group">
<label for="">Price</label>
<input type="text" class="form-control" id="" name="price">
</div>
<div class="form-group">
<label for="">Status</label>
<select class="form-control input-sm m-bot15" id="" name="status">
<option value="0">Inactive</option>
<option value="1">Active</option>
</select>
</div>
<div class="form-group">
<label for="img">Image</label>
<div class="input-group">
<span class="input-group-btn">
<span class="btn btn-default btn-file">
Choose <input type="file" id="imgInp">
</span>
</span>
<input type="text" class="form-control" name="product_image" readonly>
</div>
<img id='img-upload' class="image_responsive" />
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label for="">Description</label>
<textarea class="form-control" id="" name="description"></textarea>
</div>
<div class="form-group">
<label for="">Note</label>
<textarea class="form-control" id="" name="addition_information"></textarea>
</div>
</div>
</div>
</div>
<div class="modal-footer">
<input type="submit" value="Save" class="btn btn-success">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</form>
</div>
</div>
</div>
I have implemented google re captcha in a modal popup. I have also included the api ,i.e recaptcha/api.js to enable the recpatcha. But it is not showing up when I invoke the popup.
When I right click on the popup and inspect, the div tag which includes the google recaptcha is being seen in the elements section .
I have included the modal popup in my footer page which is called by every page in my website.
My code:
<script src="https://www.google.com/recaptcha/api.js"></script>
<!-- Request A Demo POPUP(Start) -->
<div class="modal fade" id="myModal" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<?php echo form_open(base_url( 'employer-request-demo/request-demo/submit' ), array( 'id' => 'idfrmdemo', 'name' =>'frmdemo', 'method'=>'post'));?>
<!-- input type="text" name="<?php //echo $this->security->get_csrf_token_name();?>" value="<?php //echo $this->security->get_csrf_hash();?>"-->
<div class="modal-content">
<div class="modal-header" align="center">
<button type="button" id="frmdemo-close" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title"><strong>Request a Demo</strong></h4>
</div>
<div class="modal-body" id="idmodalcontent">
<div class="row">
<div class="form-group col-md-6">
<label for="txtfirstname">FIRST NAME</label>
<input name="txtfirstname" type="text" class="form-control required" placeholder="Please enter your first name" id="idfirstname" >
</div>
<div class="form-group col-md-6">
<label for="txtlastname">LAST NAME</label>
<input name="txtlastname" type="text" class="form-control required" placeholder="Please enter your last name" id="idlastname">
</div>
</div>
<div class="form-group">
<label for="Email">CORPORATE EMAIL</label>
<input type="email" name="txtemail" class="form-control" placeholder="Please enter your email id" id="idemail">
<div class="divstyle"></div>
</div>
<div class="form-group">
<label for="idcompany">COMPANY NAME</label>
<input type="text" name="txtcompany" placeholder="Please enter the name of your company" class="form-control" id="idcompany" >
</div>
<div class="form-group">
<label for="Phone">PHONE</label>
<input type="tel" name="txtphone" placeholder="Please enter your phone number" class="form-control" id="idphone">
</div>
<div class="form-group">
<label for="">MODE OF CONTACT</label>
<select name="selmode" id="idmode" class="form-control">
<option value="" disabled selected>Select mode of contact</option>
<option value="email">Email</option>
<option value="phone">Phone</option>
<option value="any">Any</option>
</select>
</div>
<div class="form-group">
<div class="g-recaptcha" data-sitekey="6LexRREUAAAAAELhZaiO5FxRbgnyWEwIxFTWeHhE"></div>
<input type="hidden" class="hiddenRecaptcha required" name="cphiddenRecaptcha" id="idcphiddenRecaptcha">
</div>
</div>
<div class="modal-footer">
<div id="btn-smb">
<input type="submit" id="idsubmit" name="submit" value="Submit" class="btn btn-warning" />
</div>
<div id="btn-close" style="display:none;">
<input type="button" name="close" value="Close" class="btn btn-warning" id="idclose"/>
</div>
</div>
</div>
<input type="hidden" name="context" id="frm_context" value="Request A Demo" />
<?php echo form_close();?>
</div>
</div>
<!-- Request A Demo POPUP(End) -->
I am getting the following error in the console:
XMLHttpRequest cannot load javascript:void(0);. Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https, chrome-extension-resource.