Cannot load data from PHP + JQuery to Bootstrap 3 Modal - php

Briefly speaking, my site has a Bootstrap modal, which is used for display a Youtube video, like following
<!-- Video dialog-->
<div class="modal fade" id="vid-modal">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<div class="modal-title">
<h4 id="modal-title">Modal title</h4>
</div>
</div>
<div class="modal-body">
<div class="modal-video">
<iframe src=""></iframe> <!-- Code for video here -->`
</div>
</div>
</div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->
</div><!-- /.modal -->
<!-- End video dialog-->
This modal is displayed each time user clicks on a link. Those links are loaded from database using PHP with this code:
<div class="panel-group" id="accordion">
<?php
$v_units = $courses->get_distinct_unit($id);
foreach ($v_units as $v_unit) { ?>
<div class="panel-info">
<div class="panel-heading">
<h4 class="panel-title">
<a data-toggle="collapse" href="#collpase<?php echo $v_unit['unit_id'];?>"><?php echo $v_unit['unit_id'] . ' - ' . $v_unit['unit_name']; ?></a>
</h4>
</div> <!-- End of .panel-heading-->
<div id="collpase<?php echo $v_unit['unit_id'];?>" class="panel-collapse collapse in">
<div class="panel-body">
<?php $v_vids = $courses->get_unit_video($v_unit['unit_id']); ?>
<?php foreach ($v_vids as $v_vid): ?>
<a class="show-vid-modal" href="<?php echo $v_vid['vid_link']; ?>"><?php echo $v_vid['vid_title'] ?></a>
<?php endforeach ?>
</div> <!-- End of .panel-body -->
</div> <!-- End of .panel-collapse -->
</div> <!-- End of .panel-default-->
<?php }?>
</div> <!-- End of #accordion-->
The a class="show-vid-modal" ... triggers the modal.Modal's title and video link is fetched through PHP for each item.
I have added the following javascript code.
<script>
$('.show-vid-modal').click(function(e) {
e.preventDefault();
$("#vid-modal").on('show.bs.modal', function(e) {
$("h4 #modal-title").text("<?php echo $v_vid['vid_title'];"?>);
$("div .modal-video").innerHTML = "<iframe src="<?php echo $v_vid['vid_link'];?>" "
});
$("#vid-modal").modal();
});
</script>
However, it only shows the title and video of the last item for every item (as the foreach loop ends). If I put the javascript inside foreach loop, then it will not work.
Can anyone give me an idea on how I should do that.

In your modal, put an id for the iframe, so it'll be:
<iframe id="video-frame" src=""></iframe>
In your link, create a custom attribute, let's say video-src, so it'll be:
<a class="show-vid-modal" href="#" video-src="<?php echo $v_vid['vid_link']; ?>"><?php echo $v_vid['vid_title'] ?></a>
Once you have it, put in your button's click function:
$('.show-vid-modal').click(function(e) {
//here the attribute video-src is helpfull
var vid-src = $(this).attr('video-src');
var title = $(this).html();
e.preventDefault();
$("#vid-modal").on('show.bs.modal', function(e) {
$("h4 #modal-title").text(title);
//here the id for the iframe is helpfull
$('#video-frame').attr('src', vid-src);
});
$("#vid-modal").modal();
});
It might work.

Related

Regarding HTML5canvas div images to download individually

I need help i want to download the htmlf 5 canvas for each individuall and im calling the image data dynamic but not been able to download the canvas plzz help me out from such informartion through my code in future user may add as many template images it will be called with same design as i have called data dynamically so im not getting what to do for download the canvas for such div images can anyone help me out
if ($sImage)
{
$gallery = get_field('field_5ea2b74754193');
?>
<div class="row">
<?php
$counter=1;
$canvas=1;
foreach ($gallery as $value) { ?>
<div class="col-sm-4">
<div class="onethird">
<center>
<h2>Design-<?php echo $counter++;?></h2><br>
<div class="my_banner" id="myCanvas <?php echo $canvas; ?>">
<div class="template_bg" style="background-image: url(<?php echo $sImage?>);">
<!--php echo '<img src="'.$sImage.'" class="absolute img-fluid" />'; ?-->
<img src="<?php echo $value['full_image_url'];?>" class="relative img-fluid" />
</div>
</div>
<div>
<br>
<a id="download" href="#" class="dowload btn btn-danger">Download</a>
</div>
</center>
</div>
</div>
<?php $canvas++; } ?>
<script>
$(document).ready(function() {
var canvas_id=0;
$('a.dowload').click(function(e){
e.preventDefault();
alert('Download on process');
html2canvas($('#myCanvas'),{
onrendered: function (canvas) {
var a = document.createElement('a');
a.href = canvas.toDataURL("image/png");
alert(a.href);
a.download = 'my-image.png';
a.click();
}
});
});
});
</script>
</div>
</div>
<?php
}
I have used a modal box for my issue to get the work to be done in proper work for converting Div images to html2canvas to create one image and download the image side by side
if ($sImage)
{
$gallery = get_field('field_5ea2b74754193');
?>
<div class="row">
<?php
$counter=1;
foreach ($gallery as $key=> $value) {?>
<div class="col-sm-4">
<div class="onethird">
<center>
<h2>Design-<?php echo $counter++;?></h2><br>
<div class="my_banner" id="myCanvas_<?php echo $value['id']; ?>">
<div class="template_bg" style="background-image: url(<?php echo $sImage?>);">
<img src="<?php echo $value['full_image_url'];?>" class="relative img-fluid" />
</div>
</div>
<div>
<br>
<!-- Preview -->
<div class="qrcimg mt-3" data-toggle="modal" data-target="#myModal">
<input id="preview_box" type="button" data-id="<?php echo $value['id'];?>" name="canvas_id" value="Get Image" class="preview_box btn btn-outline-primary"/>
</div>
<div class="modal fade" id="myModal" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title text-center" style="color:black; font-weight:bold;">SlashDP</h4>
<button type="button" class="close" data-dismiss="modal" style="font-size:2rem;">×</button>
</div>
<div class="modal-body">
<div id="canvas_image" data-id="<?php echo $value['id'];?>"></div>
<center><a id="download" href="#" class="clear btn btn-danger"><i class="fa fa-download" style="font-size:1rem;"> Download</i></a></center>
</div>
</div>
</div>
</div>
</div>
</center>
</div>
</div>
<?php } ?>
<!-- Canvas image get preview and download code -->
<script>
$(document).ready(function() {
// Global variable
var getCanvas;
$(".preview_box").on('click', function() {
var dataId = $(this).attr("data-id");
var canvas_img = $("#myCanvas_"+dataId).get(0);
html2canvas(canvas_img,{
useCORS: true,
allowTaint: false,
letterRendering: true,
logging:true,
onrendered: function(canvas) {
$("#canvas_image").append(canvas);
getCanvas = canvas;
console.log(canvas);
}
});
});
$("#download").on('click', function() {
var imageData =getCanvas.toDataURL("image/png");
// Now browser starts downloading
// it instead of just showing it
var newData = imageData.replace(/^data:image\/png/, "data:application/octet-stream");
$("#download").attr("download", "my-demo.png").attr("href", newData);
});
jQuery(".clear").click(function(){ jQuery("#canvas_image").empty();});
jQuery(".close").click(function(){ jQuery("#canvas_image").empty();});
});
</script>
</div>
</div>**

Display a bootstrap dynamic modal

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");
});
});
});

pass php variable in modal bootstrap

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>

To open pop up for every row in table

I am trying to open bootstrap pop for every row in table .It works if I keep the
onclick event in loop and pop up window in loop for getting different id ie photoModal.But for some reason i have to keep the pop up window out of loop.
<!-- Image toggle box start-->
<div class="bs-example">
<div id="photoModal" class="modal fade">
<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">rudra pop 2 </h4>
</div>
<div class="modal-body"> <img src="<?php echo URL;?>images/decor/Int-Design2.jpg" style="max-width:100%; " /> </div>
</div>
</div>
</div>
</div>
<!-- End Image toggle box start-->
<a href="#" data-toggle="modal" data-target="#photoModal" data-title="Feedback"
style="border:none !important; text-decoration:none !important;">
<div class="tz-image-item">
<img alt="" src="images/decor/Int-Design1.jpg">
</div>
</a>
Use Javascript to open your modal dialog. That should help you.
$(document).ready(function() {
$('tr').on('click', function(ev) {
$('#my-modal').modal('show');
});
});

Modal window doesn't load propper results

I have few thumbs on the page which are loaded from mysql database. Now I want to make button view on them and when I click to open modal windows with more info.
The problem is that always is opening first item on each thumb(different id). This is what I've using. I guess the problem is in the button but can't figured out how to fix it.
foreach($pdo->query("SELECT * FROM projects ORDER BY project_id") as $row)
{
echo '<div class="portfolio-item '.$row['project_category'].'">';
echo '<div class="portfolio">';
echo '<a href="#" data-lightbox-gallery="portfolio">';
echo '<img src="'.$row['project_image'].'" alt="Portfolio 1">';
echo '<div class="portfolio-overlay hvr-rectangle-out">';
echo '<h2 class="margin-bottom-small">
<strong class="white-color bold-text"></strong>
</h2>';
echo '<div class="button" data-toggle="modal" data-target="#portfolio-4">View More</div>';
echo '</div><!-- END PORTFOLIO OVERLAY -->
</a>
</div>
<div class="modal fade contact-form" id="portfolio-4" tabindex="-1" role="dialog" aria-labelledby="portfolio-4" aria-hidden="true">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
<div class="modal-body member-info">
<div class="row">
<div class="col-md-5 col-sm-5">
<figure>
<img src="'.$row['project_image'].'" alt="">
</figure>
</div>
<div class="col-md-7 col-sm-7">
<div class="description">
<h3><strong class="bold-text">'.$row['project_overlay'].'</strong></h3>
<div class="light-text">'.$row['project_title'].'</div>
<div class="about margin-top-medium">
<p>'.$row['project_details'].'</p>
</div>
</div> <!-- *** end description *** -->
</div> <!-- *** end col-md-7 *** -->
</div> <!-- *** end row *** -->
</div> <!-- *** end modal-body *** -->
</div> <!-- *** end modal-content *** -->
</div> <!-- *** end modal-dialog *** -->
</div> <!-- *** end Contact Form modal *** -->
</div> <!-- *** end portfolio-item *** -->';
}
At a simple glance (I can't tell what plugin you are using) I would check this line : ... div class="modal fade contact-form" id="portfolio-4"
the button triggers this modal data-target="#portfolio-4">
So I would get the index of every row and append it to the id of the modal, then in the button data-target I would also append that index... (so IDs are different) Hope it helps, I did the same but using bootstrap.

Categories