I have this while loop..
while($list = $cars->fetch(PDO::FETCH_ASSOC)) {
<div class="car-box modalButton" id="tbinfo" data-title="<?php echo $list['title'] ?>" data-toggle="modal" data-src="http://url.com/car.php?q=<?php echo $list['rand_num']; ?>" data-height="<?php echo $height; ?>" data-width="<?php echo $size[0]; ?>" data-target="#preview">
<div class="modal fade" id="preview" 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">×</button>
<h4 class="modal-title" id="myModalLabel"><?php echo $list['title']; ?></h4>
</div>
<div class="modal-body">
<iframe style="display:block;" frameborder="0"></iframe>
</div>
<div class="modal-footer">
</div>
</div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->
</div><!-- /.modal -->
}
So when i click in the first car of the list my modal popup opens and shows the title normal as it is. When i click on the second car and the modal popup opens it shows me again the title of the first car instead of second car that i clicked on. I tried with jquery to take the title on #tbinfo click with no success:
$('#tbinfo').on('click',function(){
var title = $(this).attr('data-title');
$('.modal-title').html(title);
});
What can i do to fix it? any ideas?
In my project I'm using data- attributes.
Example. Look, how do I pass code to modal.
Link:
<a href="#modal-info" class="text-muted" data-toggle="modal" data-code="{{ code }}">
Modal:
<div class="modal fade" id="modal-info">
<dd name="code"></dd>
</div>
Event listener:
$('#modal-info').on('show.bs.modal', function (event) {
var button = $(event.relatedTarget); // Button that triggered the modal
var code = button.data('code');
var modal = $(this);
modal.find('[name="code"]').text(code);
}
hello you update your code like:
<?php
$i=1;
while($list = $cars->fetch(PDO::FETCH_ASSOC)) {?>
<div class="car-box modalButton tbinfo" data-title="<?php echo $list['title'] ?>" data-toggle="modal" data-src="http://url.com/car.php?q=<?php echo $list['rand_num']; ?>" data-height="<?php echo $height; ?>" data-width="<?php echo $size[0]; ?>" data-target="#preview_<?php echo $i; ?>">
<div class="modal fade" id="preview_<?php echo $i; ?>" 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">×</button>
<h4 class="modal-title" id="myModalLabel"><?php echo $list['title']; ?></h4>
</div>
<div class="modal-body">
<iframe style="display:block;" frameborder="0"></iframe>
</div>
<div class="modal-footer">
</div>
</div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->
</div><!-- /.modal -->
<?php $i++; }?>
assign each modal unique id
You just need to increment your html id attributes as its currently always going to reference the first modal created. All id's must be unique in html.
I would just add in a simple counter and append it to your id. So create a new variable, in this example $i and assign it the value of 0. In your loop, increment the value either at the beginning or end of the loop. Then append the value of $i to your modal id attribute and the modalButton's data-target attribute. So preview becomes something like preview-<?=$i;?> in all instances.
Here, something like this:
<?php
$i = 0;
while($list = $cars->fetch(PDO::FETCH_ASSOC)) {
++$i;
?>
<div class="car-box modalButton" id="tbinfo" data-title="<?php echo $list['title'] ?>" data-toggle="modal" data-src="http://url.com/car.php?q=<?php echo $list['rand_num']; ?>" data-height="<?php echo $height; ?>" data-width="<?php echo $size[0]; ?>" data-target="#preview-<?=$i;?>">
<div class="modal fade" id="preview-<?=$i;?>" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
... modal markup ...
Make sure your PHP installation supports the shorthand echo <?=$VAR;?>, if that isn't working then you can just echo the variable out as normal, like you've done else where <?php echo ... ?> .
That will make sure that every time you click the button, it opens a new, unique, modal instead of just the first one.
Don't use id. Use class name.
<?php
while($list = $cars->fetch(PDO::FETCH_ASSOC)) {?>
<div class="car-box modalButton" id="tbinfo" data-title="<?php echo $list['title'] ?>" data-toggle="modal" data-src="http://url.com/car.php?q=<?php echo $list['rand_num']; ?>" data-height="<?php echo $height; ?>" data-width="<?php echo $size[0]; ?>" data-target="#preview"></div>
}?>
Place this code in footer or in same page in down.
<div class="modal fade" id="preview" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
</div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->
</div><!-- /.modal -->
JS
<script>
$('.modalButton').click(function(){
var title = $(this).attr('data-title');
$.ajax({url:"ajax_modalTitle.php?title="+title,cache:false,success:function(result){
$(".modal-content").html(result);
}});
});
</script>
Create a page name ajax_modalTitle.php (If you are looking to change this page name. Change in <script></script> tag too. Both are related.)
<?php
$title = $_GET['title'];
?>
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title" id="myModalLabel"><?php echo $title;?></h4>
</div>
<div class="modal-body">
<iframe style="display:block;" frameborder="0"></iframe>
</div>
<div class="modal-footer">
</div>
Related
I have an array of locations that when a users clicks on that card it should show a modal with that specific locations data
How do I pass the data to the modal?
<!-- Card -->
<div class="locationCard" type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#exampleModal" >
<div class="locationText">
<?php echo $slide['title']; ?>
</div>
<?php echo '<img class="cardImage" >' . wp_get_attachment_image( $slide['image'], $size = 'medium',["class" => "cardImage","alt"=>"some"]) . '</img>'; ?>
</div>
<!-- Modal -->
<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel"><?php echo $slide['title']; ?></h5>
</div>
<div class="modal-body">
...
</div>
</div>
</div>
</div>
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 have a table "on php page" that contain records from a database. At each row there is a button and when I click on the button, a modal bootstrap should appear that contain details about members that you want to know.
This is the button code:
<td> <?php echo '
details'; ?> </td>
and the modal bootstrap code:
<?php
$connect = mysqli_connect('127.0.0.1','root','','smart');
$connect->set_charset("utf8");
if(isset($_GET['id'])){
$Mid = (int)$_GET['id'];
$getid = "select * from members where id = '$Mid'";
$runid = mysqli_query($connect, $getid);
$row_id = mysqli_fetch_array($runid);
$show_name = $row_id['name'];
$show_number = $row_id['number'];
?>
<div class="modal fade bd-example-modal-lg" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel" aria-hidden="true">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel"> اضافة فاتورة</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<div class="container">
<div class="row">
<form action="" method="post">
<label>الاسم</label>
<input type="textbox" name="" class="Mname" disabled value="<?php echo $show_name ?>"></input>
<label>الرقم</label>
<input type="textbox" name="" class="Mname" disabled value="<?php echo $show_number; ?>"></input>
<label>النوع</label>
<select name="dbType" id="dbType">
<option value="normal">عادي</option>
<option value="uargent">مستعجل</option>
</select>
<div id="otherType" style="display:none;">
<label for="specify">Specify</label>
<input type="text" name="specify" placeholder="Specify Databse Type"/>
</div>
</form>
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
</div>
<?php }else{echo'select an row please';} ?>
<script>
$("#myModal").modal("show");</script>
<script src="/our/js/jquery.min.js"></script>
<script src="/our/js/bootstrap.min.js"></script>
<script src="/our/js/run.js"></script>
</body>
But when I click on the add button the modal doesn't appear but the url changes like this:
http://localhost/our/dashboard.php#myModal?id=2
Why doesn't the modal work?
1. You have data-toggle='modal' but it doesn't know what modal it should open. Add this to your button.
data-target='#myModal'
Source
2. This will also trigger the modal.
$("#myModal").modal("show");
You can't use click function in a link that redirect you.
Make normal link with only href attr.
<?php echo 'Button'; ?>
url should be dashboard?id=1
html
<?php if(isset($_GET["id"])) { ?>
<div class="modal fade bd-example-modal-lg" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel" aria-hidden="true">
<!---Your same modal from your question--->
</div>
<?php } ?>
add this before your </body> closing body tag
<script>$("#myModal").modal("show");</script>
I implemented bs4, but on the modal I have a problem. In bs4 the code work but the data is not inserted. it's like a blank page.
In this case, the value of $cInfo->configuration_title, does'nt appear.
<a data-toggle="modal" data-refresh="true" href="configuration_popup.php?<?php echo 'gID='. $_GET['gID'] . '&cID=' . $Qconfiguration->valueInt('configuration_id'); ?>" data-target="#myModal_<?php echo $Qconfiguration->valueInt('configuration_id'); ?>"><?php echo HTML::image (DIR_WS_ICONS . 'edit.gif', ICON_EDIT) ?></a>
<div class="modal fade" id="myModal_<?php echo $Qconfiguration->valueInt('configuration_id'); ?>" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog" role="dialog">
<div class="modal-content" style="padding:10px 10px 10px 10px;">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
<h4 class="modal-title" id="myModalLabel"><?php echo $cInfo->configuration_title; ?></h4>
</div>
<div class="modal-body">
<div class="te"></div>
</div>
</div> <!-- /.modal-content -->
</div><!-- /.modal-dialog -->
</div><!-- /.modal -->