i'm currently working on a project to improve my PHP/SQL skills.
So basically i want to make it so you click on the news title opens a modal that shows the news content, this is what i currently have and it isn't working.
Also sorry for my english, I'm dutch.
$newssql = $odb -> query("SELECT * FROM `news` ORDER BY `date` DESC LIMIT 4");
while($row = $newssql ->fetch())
{
$id = $row['ID'];
$title = $row['title'];
$content = $row['content'];
$autor = $row['author'];
echo '
<a data-toggle="modal" data-id="'.$id.'" data-target="#modal-3">
<div class="inbox-item">
<p class="inbox-item-author">'.htmlspecialchars($title).'</p>
<p class="inbox-item-text">'.htmlspecialchars($content).'</p>
<p class="inbox-item-date">'.date("m/d/y" ,$row['date']).'</p>
</div>
</a>
';
}
//Modal
<div aria-hidden="true" aria-labelledby="modal-label-3" class="modal fade"
id="modal-3" role="modal" style="display: none;" tabindex="-1">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-header">
<button aria-hidden="true" class="close" data-dismiss=
"modal" type="button">×</button>
<h4 class="modal-title" id="modal-label-3">NEWS</h4>
</div>
<div class="modal-body">
<?php echo htmlspecialchars($content); ?>
</div>
</div>
</div>
</div>
//jQuery
<script>
$('button[data-target="#modal-3"]').click(function(event) {
event.preventDefault();
var j = $(this).attr('data-id');
});
</script>
as far as I can see,
<script>
$('button[data-target="#modal-3"]').click(function(event) {
event.preventDefault();
var j = $(this).attr('data-id');
});
</script>
jquery will detect a click event from a button where as you stated
echo '
<a data-toggle="modal" data-id="'.$id.'" data-target="#modal-3">
<div class="inbox-item">
<p class="inbox-item-author">'.htmlspecialchars($title).'</p>
<p class="inbox-item-text">'.htmlspecialchars($content).'</p>
<p class="inbox-item-date">'.date("m/d/y" ,$row['date']).'</p>
</div>
</a>
';
it's on an <a> tag which is not a button.
Related
id
product_title
product_desc
1000
Iphone 12
this is iPhone 12
1001
Iphone 11
this is iPhone 11
1002
Iphone X
this is iPhone X
1003
Iphone 8
this is iPhone 8
1004
Iphone 7
this is iPhone 7
I have a table called products that looks something like the table above. I displayed all the products on my page. But now I want to use bootstrap modal to display the product description when I clicked on the name of that particular product.( Exp: I click on the product name of iPhone 12 on my page. A boostrap modal will be displayed to show me the description of iPhone 12. )
My question is how can I display the description of that particular product that I click on? Now I am having an output of all the descriptions of the products in my modal.
This is the codes where I use to display the products
<div class="row form-group" id="myDIV">
<?php
$sql = "SELECT * FROM products WHERE product_status = 1 ORDER BY product_created DESC LIMIT 6";
$query = $conn->query($sql);
if (!mysqli_num_rows($query)) {
echo '
<div class="col-12">
<div class="badge badge-danger">No Products Found</div>
</div>
';
} else {
while ($row = $query->fetch_assoc()) {
?>
<div class="col-lg-3 col-md-4 col-6 form-group" style="padding: 0 8px 0 8px;">
<div class="product-wrapper">
<div class="card-body">
<h5 class="product-title" style="min-height: 39px;">
<span class="float-left" data-toggle="modal" data-target="#productdesc1">
<?php echo $row['product_title']; ?>
</span>
</h5>
View More
</div>
</div>
</div>
<?php
}
}
?>
</div>
These are the codes that I use for my bootstrap modals
<!-- The Modal -->
<div class="modal fade" id="productdesc1">
<div class="modal-dialog modal-dialog-centered">
<div class="modal-content">
<!-- Modal body -->
<div class="modal-body">
<div class="float-right mb-3">
<button type="button" class="close" data-dismiss="modal" style="color:#ed1c25;">×</button>
</div>
<?php
$sql = "SELECT product_desc FROM products";
$query = $conn->query($sql);
if (!mysqli_num_rows($query)) {
echo '
<div class="col-12">
<div class="badge badge-danger">No Products Found</div>
</div>
';
} else {
while ($row = $query->fetch_assoc()) {
?>
<div class="mb-3">
<h5 class="product-title">Description</h5>
<p><?php echo $row['product_desc']; ?></p>
</div>
<?php
}
}
?>
</div>
</div>
</div>
</div>
i will show you make simple with jquery
first i add onlick ShowModalProduct() at your <a> and will look like this
View More
<?=$row['id'];?> its from foreach the data and will be parameter for show product_desc at javascript code
and then at the modal i will give id at each <div>
<div class="mb-3 hidden" id="products_<?=$row['id'];?>">
<h5 class="product-title">Description</h5>
<p><?php echo $row['product_desc']; ?></p>
</div>
and i use this jquery library
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
and add the script the function ShowModalProduct() will look like this, when the function fired, it will remove class hidden and add class show-desc
function ShowModalProduct(id){
$('#products_'+id).removeClass('hidden');
$('#products_'+id).addClass('show-desc');
$('#productdesc1').modal('toggle');
}
i forget to mention i add 2 class style to show/hide the div section, and when foreach the product desc first i add class hidden like this <div class="mb-3 hidden" id="products_<?=$row['id'];?>"> and include id at the sql (SELECT id,product_desc FROM products)
<style type="text/css">
.hidden{
display: none;
}
.show-desc{
display: block;
}
</style>
and i add onclick CloseModal() at button close modal like this
<button type="button" class="close" data-dismiss="modal" onclick="CloseModal();" style="color:#ed1c25;">×</button>
and then add the function script like this
function CloseModal(){
$(".show-desc").each(function(){
var id = $(this).attr('id');
$("#"+id).removeClass('show-desc');
$("#"+id).addClass('hidden');
});
$('#productdesc1').modal('hide');
}
so when the modal close, it will change all class show-desc to class hidden so when you showing another modal, the previous div not showing again
full code will look like this
<!DOCTYPE html>
<html>
<head>
<title>test</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap#5.0.0-beta2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-BmbxuPwQa2lc/FVzBcNJ7UAyJxM6wuqIj61tLrc4wSX0szH/Ev+nYRRuWlolflfl" crossorigin="anonymous">
</head>
<style type="text/css">
.hidden{
display: none;
}
.show-desc{
display: block;
}
</style>
<body>
<div class="row form-group" id="myDIV">
<?php
$sql = "SELECT * FROM products WHERE product_status = 1 ORDER BY product_created DESC LIMIT 6";
$query = $conn->query($sql);
if (!mysqli_num_rows($query)) {
echo '
<div class="col-12">
<div class="badge badge-danger">No Products Found</div>
</div>
';
} else {
while ($row = $query->fetch_assoc()) {
?>
<div class="col-lg-3 col-md-4 col-6 form-group" style="padding: 0 8px 0 8px;">
<div class="product-wrapper">
<div class="card-body">
<h5 class="product-title" style="min-height: 39px;">
<span class="float-left" data-toggle="modal" data-target="#productdesc1">
<?php echo $row['product_title']; ?>
</span>
</h5>
View More
</div>
</div>
</div>
<?php
}
}
?>
</div>
<!-- The Modal -->
<div class="modal fade" id="productdesc1" role="dialog">
<div class="modal-dialog modal-dialog-centered">
<div class="modal-content">
<!-- Modal body -->
<div class="modal-body">
<div class="float-right mb-3">
<button type="button" class="close" data-dismiss="modal" onclick="CloseModal();" style="color:#ed1c25;">×</button>
</div>
<?php
$sql = "SELECT id,product_desc FROM products";
$query = $conn->query($sql);
if (!mysqli_num_rows($query)) {
echo '
<div class="col-12">
<div class="badge badge-danger">No Products Found</div>
</div>
';
} else {
while ($row = $query->fetch_assoc()) {
?>
<div class="mb-3 hidden" id="products_<?=$row['id'];?>">
<h5 class="product-title">Description</h5>
<p><?php echo $row['product_desc']; ?></p>
</div>
<?php
}
}
?>
</div>
</div>
</div>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap#5.0.0-beta2/dist/js/bootstrap.bundle.min.js" integrity="sha384-b5kHyXgcpbZJO/tY9Ul7kGkf1S0CWuKcCD38l8YkeH8z8QjE0GmW1gYU5S9FOnJ0" crossorigin="anonymous"></script>
<script type="text/javascript">
function ShowModalProduct(id){
$('#products_'+id).removeClass('hidden');
$('#products_'+id).addClass('show-desc');
$('#productdesc1').modal('toggle');
}
function CloseModal(){
$(".show-desc").each(function(){
var id = $(this).attr('id');
$("#"+id).removeClass('show-desc');
$("#"+id).addClass('hidden');
});
$('#productdesc1').modal('hide');
}
</script>
</body>
</html>
if use ajax, change the function at script like this
function ShowModalProduct(id){
$.ajax({
url: 'GetProduct.php',
type: 'get',
data: {'id':id},
dataType: 'JSON',
success: function(response){
if(response==null){
alert('Data Not Found');
}else{
data = response;
$('#desc-product').html(data.product_desc);
$('#productdesc1').modal('toggle');
}
}
});
}
function CloseModal(){
$('#productdesc1').modal('hide');
}
and create new php file GetProduct.php for get detail product
<?php
$id_product = $_GET['id'];
$conn = mysqli_connect("localhost", "root", "","test"); //your config connection
$sql = "SELECT * FROM products WHERE product_status = 1 and id='".$id_product."' ";
$result = $conn->query($sql);
$row = $result->fetch_assoc();
echo json_encode($row);
?>
full code at main file will like this
<!DOCTYPE html>
<html>
<head>
<title>test</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap#5.0.0-beta2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-BmbxuPwQa2lc/FVzBcNJ7UAyJxM6wuqIj61tLrc4wSX0szH/Ev+nYRRuWlolflfl" crossorigin="anonymous">
</head>
<!--
<style type="text/css">
.hidden{
display: none;
}
.show-desc{
display: block;
}
</style>
-->
<body>
<div class="row form-group" id="myDIV">
<?php
$conn = mysqli_connect("localhost", "root", "","test"); //your config connection
$sql = "SELECT * FROM products WHERE product_status = 1 ORDER BY product_created DESC LIMIT 6";
$query = $conn->query($sql);
if (!mysqli_num_rows($query)) {
echo '
<div class="col-12">
<div class="badge badge-danger">No Products Found</div>
</div>
';
} else {
while ($row = $query->fetch_assoc()) {
?>
<div class="col-lg-3 col-md-4 col-6 form-group" style="padding: 0 8px 0 8px;">
<div class="product-wrapper">
<div class="card-body">
<h5 class="product-title" style="min-height: 39px;">
<span class="float-left" data-toggle="modal" data-target="#productdesc1">
<?php echo $row['product_title']; ?>
</span>
</h5>
View More
</div>
</div>
</div>
<?php
}
}
?>
</div>
<!-- The Modal -->
<div class="modal fade" id="productdesc1" role="dialog">
<div class="modal-dialog modal-dialog-centered">
<div class="modal-content">
<!-- Modal body -->
<div class="modal-body">
<div class="float-right mb-3">
<button type="button" class="close" data-dismiss="modal" onclick="CloseModal();" style="color:#ed1c25;">×</button>
</div>
<div class="mb-3">
<h5 class="product-title">Description</h5>
<p id="desc-product"></p>
</div>
</div>
</div>
</div>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap#5.0.0-beta2/dist/js/bootstrap.bundle.min.js" integrity="sha384-b5kHyXgcpbZJO/tY9Ul7kGkf1S0CWuKcCD38l8YkeH8z8QjE0GmW1gYU5S9FOnJ0" crossorigin="anonymous"></script>
<script type="text/javascript">
function ShowModalProduct(id){
$.ajax({
url: 'GetProduct.php',
type: 'get',
data: {'id':id},
dataType: 'JSON',
success: function(response){
if(response==null){
alert('Data Not Found');
}else{
data = response;
$('#desc-product').html(data.product_desc);
$('#productdesc1').modal('toggle');
}
}
});
}
function CloseModal(){
$('#productdesc1').modal('hide');
}
</script>
</body>
</html>
You can do this by two ways:
Set the modal in the item list, inside the foreach block, and but the item description inside the modal, but don't forget to change modal ID regarding the toggle button id.
Call an onclick event when user clicks on item name, the action accociated with this event calls ajax request to get element by ID from database, when the request success, set the retrieved data inside a modal and make it visible.
After searching a lot, doing lots of debugging, and I figured out that inside slick carousel div, the bootstrap modal is not displaying. Only showing a gray dropback.
Here is code: In this code, we are getting details from the database and with slick showing images and when someone clicks on those images it will open a modal for that image. But everything is fine except this. How to call the modal inside slick div.
Thank you in advance
<?php
$ush = $mysqli->prepare("SELECT company_id, img, name from list");
$ush->execute();
$ush->store_result();
$ush->bind_result($u_bid, $u_bimg,$u_bname);
?>
<div class="company-logo">
<?php
while($ush->fetch()){
?>
<div>
<a href="#<?php echo $u_bid;?>" data-toggle="modal" data-target="#<?php echo $u_bid;?>"><img
src="data:image/png;base64,<?php echo base64_encode($u_bimg); ?>" height="80px" width="80px"></a>
<!-- Modal -->
<div class="modal" id="<?php echo $u_bid;?>" tabindex="-1" role="dialog" aria-labelledby="<?php echo $u_bid;?>" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered modal-md" role="document">
<div class="modal-content">
<div class="modal-body">
<div class="container text-center">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true"><h3><b>X</b></h3></span>
</button>
<h3><?php echo $u_bname;?></h3>
</div>
</div>
</div>
</div>
</div>
</div>
<?php
}
$ush->close();
?>
</div>
<script type="text/javascript">
$(document).ready(function(){
$('.company-logo').slick({
dots:true,
infinite:false,
speed:300,
slidesToShow:14,
slidesToScroll:14,
arrows:false,
responsive:[{
breakpoint:768,
settings:{
slidesToShow:4,
slidesToScroll:4
}
}]
})
});
</script>
I think you should move the modal code parts outside of your carousel parts like this:
<?php
$ush = $mysqli->prepare("SELECT company_id, img, name from list");
$ush->execute();
$ush->store_result();
$ush->bind_result($u_bid, $u_bimg,$u_bname);
$carousel_content = '';
$modal_content = '';
while($ush->fetch()){
$carousel_content .= '<div>
<a href="#'.$u_bid.'" data-toggle="modal" data-target="#'.$u_bid.'"><img
src="data:image/png;base64,'.base64_encode($u_bimg).'" height="80px" width="80px"></a>
</div>';
$modal_content .= '<!-- Modal -->
<div class="modal" id="'.$u_bid.'" tabindex="-1" role="dialog" aria-labelledby="'.$u_bid.'" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered modal-md" role="document">
<div class="modal-content">
<div class="modal-body">
<div class="container text-center">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true"><h3><b>X</b></h3></span>
</button>
<h3>'.$u_bname.'</h3>
</div>
</div>
</div>
</div>
</div>';
?>
<?php
}
$ush->close();
?>
<div class="company-logo"><?=$carousel_content?></div>
<div id="modal_contents"><?=$modal_content?></div>
<script type="text/javascript">
$(document).ready(function(){
$('.company-logo').slick({
dots:true,
infinite:false,
speed:300,
slidesToShow:14,
slidesToScroll:14,
arrows:false,
responsive:[{
breakpoint:768,
settings:{
slidesToShow:4,
slidesToScroll:4
}
}]
})
});
</script>
so i have a while loop and i am fethcing the name and the description of them from my db
so when ever i click on one of the parts i want the modal to display the name of the item that i clicked on in the modal i hope this picture would describe it better
below is the code i have so far
at the moment when i click on the modal i get displayed the name of the item which is first on the list no matter where i click
while($row = mysqli_fetch_assoc($result))
{
$name= $row['name_of_product'];
$description = $row['description']
?>
<a href="#" data-toggle="modal" data-target="#mymodal">
<div class="col-sm-4">
<?php echo name; ?>
<?php echo description ; ?>
</div>
</a>
<div id="mymodal" class="modal fade" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Modal Header</h4>
</div>
<div class="modal-body">
<p><?php echo $name; ?></p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
<?php } ?>
You are not doing things the way you have to do. You are re-creating your mymodal for every iteration of your while loop which is not a better way to achieve what you want.
Follow these steps:
Create your mymodal outside of your while loop.
Pass the ID of current row to a javascript function and populate the data of that id using javascript.
I have set the things which needs to be done. Try the following code
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<!--- display table header row-->
<table style="width:100%">
<tr>
<th>name</th>
<th>description</th>
<th>Action</th>
</tr>
<?php
while($row = mysqli_fetch_assoc($result))
{
$name= $row['name_of_product'];
$description = $row['description'];
$id = $row['id']; // I am assuming that your table has auto incremented primary key column by the name of id, if not then replace that by $row['your_id_column']
?>
<!--- desplay the data in a table row -->
<tr>
<td id="<?php echo "name_".$id; ?>"><?php echo $name; ?></td>
<td id="<?php echo "description_".$id; ?>"><?php echo $description ; ?></td>
<td><div href="#" data-toggle="modal" data-target="#mymodal" onClick="showModal(<?php echo $id ; ?>)">Edit</div></td>
</tr>
<?php } ?>
</table>
<!--- Your modal -->
<div id="mymodal" class="modal fade" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Modal Header</h4>
</div>
<div class="modal-body">
<p id="name"></p> <!--- name will be shown here-->
<p id="description"></p><!--- description will be shown here-->
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
<script>
function showModal(id)
{
// setting the values of clicked item in the bootstrap Modal
$("#name").text($("#name_"+id).text());
$("#description").text($("#description_"+id).text());
}
</script>
Looks like you are not serializing the modals... If you wanted to do things that way your code should be something like;
<!-- language-all: lang-html -->
while($row = mysqli_fetch_assoc($result)) {
$procuct_id = $row['ID'];
$name = $row['name_of_product'];
$description = $row['description']
?>
<a href="#" data-toggle="modal" data-target="#mymodal_<?php echo $procuct_id;?>">
<div class="col-sm-4">
<?php echo name; ?>
<?php echo description ; ?>
</div>
</a>
<div id="mymodal_<?php echo $procuct_id;?>" class="modal fade" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Modal Header</h4>
</div>
<div class="modal-body">
<p><?php echo $name; ?></p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
<?php }
<--
A Better way to do it would be to write a javascript function to show the generic modal window, then run an ajax call to load your info into the "modal-body" div. You would have to pass a unique id to the call in order to have your ajax script hit the db and get the info to display.
It's been almost 4 years but I'm writing for everyone; change data-target(id) as row id.
edited code:
while($row = mysqli_fetch_assoc($result))
{
$name= $row['name_of_product'];
$description = $row['description']
?>
<a href="#" data-toggle="modal" data-target="#<?php echo $row['id']; ?>">
<div class="col-sm-4">
<?php echo name; ?>
<?php echo description ; ?>
</div>
</a>
<div id="<?php echo $row['id']; ?>" class="modal fade" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Modal Header</h4>
</div>
<div class="modal-body">
<p><?php echo $name; ?></p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
<?php } ?>
I've got a problem adding ajax to my code. I'm trying to make my photogallery so I can put in images filtered by categories trough a form. After I put in ajax, my modal turns out blank like this:
But somehow it does display the thumbnail picture. It seems to not take the target id's of the modal.
Here's my ajax code:
function fotoalbumfilter() {
$.ajax({
url: '../ajax/fotoalbumfilter.php',
data: {
categorie: $('#fotoalbumfilter').val()
},
type: "POST",
dataType: 'json'
}).done(function(result) {
var jsonResult = result;
var outputgallery = jsonResult.outputgallery;
$('#output-gallery').html(outputgallery);
}).fail(function(jqXHR, textStatus, errorThrown) {
console.log(jqXHR, textStatus, errorThrown);
});
};
$(document).ready(function() {
fotoalbumfilter();
});
my php code:
define('AJAX_REQUEST', isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest');
if(!AJAX_REQUEST) { die();}
require_once('../app_config.php');
if($_POST['categorie']){ $categorie = $_POST['categorie']; }else{ $categorie = ''; }
$result = array();
if($categorie == ''){ $fotos = ''; }else{ $fotos = "WHERE (categorie_id = '".$categorie."')"; }
$r = 0;
$querycategorie = mysqli_query($con, "SELECT * FROM fotos ".$fotos." ORDER BY timestamp ASC");
while($uitkomst = mysqli_fetch_array($querycategorie)){
$weergave = 1;
$titel = $uitkomst['titel'];
$foto = $uitkomst['naam'];
$r++;
if($r == 1){ $outputgallery = '<div class="row">'; }
$outputgallery .= '<div class="col-lg-3 col-sm-4 col-md-4 col-xs-6 fotocontaineralbum">
<a class="thumbnail" href="#" data-image-id="" data-toggle="modal" data-title="'.$titel.'" data-image="/images/fotoalbum/'.$foto.'" data-target="#image-gallery">
<img class="img-fluid" src="/images/fotoalbum/'.$foto.'">
</a></div>';
if($r == 4){ $outputgallery .= '</div>'; $r = 0; }
}
if($r !== 0){ $outputgallery .= '</div>'; }
$outputgallery = utf8_encode($outputgallery);
$result['outputgallery'] = $outputgallery;
echo json_encode($result);
and my html (with php dropdown):
<div class="container">
<div class="col-md-12 pagecontainer container">
<div class="row">
<div class="col-md-12">
<center><h2><span class="impactfont mcorange" style="margin:20px;">Het Fotoalbum</span></h2></center>
</div>
</div>
<div class="row">
<div class="col-md-4">
<div class="categoriecontainer impactfont">
<select class="categoriedropdown" onchange="fotoalbumfilter()" id='fotoalbumfilter'>
<option>Kies een categorie</option>
<?php
mysqli_query($con, "SET CHARACTER SET utf8_general_ci");
$query = mysqli_query($con, "SELECT * FROM foto_categorie ORDER BY 'id' ASC") or die (mysqli_error($con));
while($uitkomst = mysqli_fetch_array($query)){
$categorie = $uitkomst["categorie"];
$id = $uitkomst["id"];
?>
<option value="<?php echo $id; ?>"><?php echo $categorie;?></option>
<?php } ?>
</select>
</div>
</div>
</div>
<div class="col-md-12 padding20 pagecontent">
<div id="output-gallery">
</div>
</div>
</div>
</div>
<div class="modal fade" id="image-gallery" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title" id="image-gallery-title"></h4>
<button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">×</span><span class="sr-only">Close</span></button>
</div>
<div class="modal-body">
<img id="image-gallery-image" class="img-fluid" src="">
</div>
<div class="modal-footer">
<div class="col-md-2 floatl">
<button type="button" class="btn btn-primary floatl" id="show-previous-image">Previous</button>
</div>
<div class="col-md-8 text-justify" id="image-gallery-caption">
</div>
<div class="col-md-2 floatr">
<button type="button" id="show-next-image" class="btn btn-default floatr">Next</button>
</div>
</div>
</div>
</div>
</div>
The issue is that you do not instruct the modal to update its contents. You can do so by adding the following javascript next to your existing one. Basically this updates the contents of the modal depending on which button was clicked. The functionality is explained in detail in the Bootstrap docs under the Varying modal content section.
$('#image-gallery').on('show.bs.modal', function (event) {
var thumbnail = $(event.relatedTarget),
imageSrc = thumbnail.data('image');
$('#image-gallery-image').attr('src', imageSrc);
});
I am trying to display a modal when pressing the button "Vezi rezolvare"
I have the following code for the button:
<div class="col-md-4 text-center patrat-block">
<div class="thumbnail">
<img class="img-responsive" src="img/LogoProbleme/pg1logo-v2.jpg" alt="">
<div class="caption">
<h3>Problema 1<br>
<small>Gradina</small>
</h3>
<p>Află dimensiunile grădinii</p>
<ul class="list-inline">
<li>
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#Problema"><span class="glyphicon glyphicon-pencil"></span> Vezi Rezolvare </button>
</li>
</ul>
</div>
</div>
</div>
And the modal content + php :
<?php
if(isset($_GET['id'])&&isset($_GET['category']))
{
$id=$_GET['id'];
$category=$_GET['category'];
$sql = "SELECT cerinta, rezolvare FROM probleme WHERE id='".$id."'AND category='".$category."'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc())
{
$cerinta=$row["cerinta"];
$rezolvare=$row["rezolvare"];
}
$_SESSION["cerinta"]=$cerinta;
$_SESSION["rezolvare"]=$rezolvare;
}
}
?>
<div id="Problema" class="modal fade" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Problema</h4>
</div>
<div class="modal-body">
<h4>Cerinta</h4>
<div class="well">
<?php echo $cerinta;?>
</div>
<h4>Rezolvare</h4>
<div class="well">
<?php echo $rezolvare;?>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Inchide fereastra</button>
</div>
</div>
</div>
</div>
What this is trying to accomplish is to show a modal with some information in my database. I checked and the values are stored successfully in $cerinta and $rezolvare.
It seems like when the button is pressed, there is a fast fade and I am redirected to the top of the page, without showing the modal. I put an <a href="probleme.php?id=1&category=g"> so I know what button has been pressed, but it seems like it refreshes the page and the modal isn't displayed. How can I let the modal be displayed?
Nesting a <button> inside an achor <a> is not valid html syntax because it can't tell which one was clicked. Also the anchor has an href different than '#' so it's just redirecting you to probleme.php?id=1&category=g. You can modify your code as follows (not tested):
Remove the button from inside the anchor and change the anchor to:
<a href="probleme.php?id=1&category=g" class="btn btn-primary"
data-toggle="modal" data-target="#Problema">
<span class="glyphicon glyphicon-pencil"></span> Vezi Rezolvare
</a>
Have the template of the modal somewhere in your html:
<div id="Problema" class="modal fade" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Problema</h4>
</div>
<div class="modal-body">
Some fine body
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">
Inchide fereastra
</button>
</div>
</div>
</div>
</div>
Modify your php to return only the body of the modal:
<?php
// Some good php
echo("
<h4>Cerinta</h4>
<div class=\"well\">
{$cerinta)}
</div>
<h4>Rezolvare</h4>
<div class=\"well\">
{$rezolvare}
</div>");
// The modal body is returned by php
?>
Use ajax to retrieve the modal body and subsequently open the modal. Something in the lines of:
$(document).ready(function() {
// Assign some id to the anchor say id="myAnchor"
$('#myAnchor1').click(function(e) {
e.preventDefault();
// get the modal
var modal = $($(this).data("data-target"));
var url = $(this).attr("href");
var request = $.get(url);
request.done(function(response) {
// get modal
modal.find(".modal-body").html(response);
modal.modal("show");
});
});
});