jQuery Dialogue box is not working properly for dynamic data from database. I want to open dialogue box for multiple data fetched from database. But the problem is that when I click any of the link to open particular dialogue box for particular id of database table record but it opens all the previous id's dialogue boxes also.
For example if I click on id 2 dialogue box it opens 1 and dialogue boxes simultaneously. I want some download in dialogue box also.
jQuery code:
$(document).ready(function() {
$(function() {
$(".dialog").dialog({
autoOpen: false,
maxWidth:600,
maxHeight: 500,
width: 600,
height: 300,
dialogClass: 'main-dialog-class',
modal: true
});
$("a.To").on("click", function() {
$(".dialog").dialog("open");
});
});
PHP code:
<table>
<?php foreach($tList as $ts) : ?>
<div class="dialog" title="Dialog Form">
<?php
$sql1="select * from table where ID='".$ts["ID"]."'" ;
$result1=mysqli_query($link,$sql1);
while($rows=mysqli_fetch_array($result1)){
echo $rows["t1"];
?>
<?php echo $rows['Name'];?><br/>
<?php
}
?>
</div>
<tr>
<td style="display:none">
<?php echo $ts["ID"]; ?>
</td>
<td>
<a href="#" class="To" >
<?php echo $ts["Title"]; ?></a>
</td>
<td>
<?php echo $ts["t1"]; ?>
</td>
<td>
<?php echo $ts["t2"]; ?>
</td>
</tr>
</table>
For starters, your HTML looks like you are nesting the dialog box in a <div> inside the table, but not in a cell (that should be an error in the browser).
But in your javascript, you need a way to specifically identify each dialog box. The error you are reporting makes sense; by choosing $('.dialog'), you are saying "pick every element on the page with a classname "dialog".
I think the easiest way is to change your JavaScript to:
$("a.To").on("click", function() {
$(this).find(".dialog").dialog("open");
});
and then move the dialog box into a cell in each row in your PHP.
Otherwise, you may choose a unique identifier for each dialog box, and rename them to an id, not a class.
EDIT
Yes, you can add the unique id in PHP for the selectors, add the same id in data and then find them with a jQuery selector.
In your PHP:
<td>
<a href="#" class="To" data-dialogfinder='<?php echo $ts["ID"]; ?>'>
<?php echo $ts["Title"]; ?>
</a>
</td>
Then, in you jQuery:
$("a.To").on("click", function() {
var diabox='#'+$(this).data("dialogfinder");
$(diabox).dialog("open");
});
I don't know if that is the full code, but in what you have posted you have missing the endforeach; (to close the foreach() : opened at the beginning).
Related
I'm trying to created a select box in my form and display the image that is selected in the select box next to it. I know it requires jquery or something and I have no clue when it comes to that. If anyone can help or point me in the right direction that would be awesome!!!
<table width="100%">
<tr>
<td>
<select id="icon" name="icon" class="form-control select2">
<?php foreach ($icons as $icon) { ?>
<option value="<?php echo $icon['link']; ?>">
<?php echo $icon['name']; ?>
</option>
<?php } ?>
</select>
</td>
<td align="center" id="iconPreview">
Image Here
</td>
</tr>
</table>
i found this jquery snippet but it doesn't display anything on change...
JQuery:
<script>
$(document).ready(function() {
$("#icon").change(function() {
$("#iconPreview").empty();
if ( $("#icon").val()!="" ){
$("#iconPreview").append("<img src=\"" + $("#icon").val() + "\" />");
}
else{
$("#iconPreview").append("displays image here");
}
});
});
</script>
You'll need to add jQuery if you want to use jQuery in the head, so in your document in the <head> add this line:
<script src="http://code.jquery.com/jquery-1.12.0.min.js"></script>
Then give the TD you want the image in a class:
<td class="imageHere" align="center" id="seleced_image">
Then, at the bottom of your page (or a .js page if you want to link it) add this:
<script>
$(function(){
$('#icon').change(function(){
var image = $('#icon').val();
$('#seleced_image').html("<img src='" + image + "' alt='description' />");
});
});
</script>
This jQuery is saying "Everytime the #icon dropdown changes, load the image into the <td>
This is the simplest way to do it without learning any jQuery. Otherwise, I suggest you learn jQuery or better yet, javascript.
Changes made:
Its better to work with IDs than classes on cases like this.
I'm building a website where the admin can make settings for the website. I would like the admin settings page to have a similar "feel" as the rest of the website, which has some nice looking jQuery features.
On the admin site there's a hidden div, which is shown when one of six links has been clicked. I'd like the content of the hidden div to change content before showing itself. I'm not sure how to do this. I could have a div box for every link on the page. But this becomes pretty cumbersome since I'd need to repeat my css and jquery for every link. I imagine that this, somehow, can be done with some javascript/jquery code that determines which link that was click and then decides which php function to call inside the hidden div. Then the php could "echo" out the content of the div, which then could be shown.
How could one do this?
My HTML/jQuery code is as follows:
--- The html links ---
<table id="settings">
<tr>
<td>
<img src="images/folder.gif" alt="" height="100"/>
</td>
<td>
<img src="images/folder.gif" alt="" height="100"/>
</td>
<td>
<img src="images/folder.gif" alt="" height="100"/>
</td>
----- The Hidden div -----
<div id="dashboard_box">
<div class="dashboard_inside">
<form action="#" method="post">
<p style="font-size:20px; font-weight: bold;">Change color</p>
</br>
<fieldset>
<? load_content1();?>
</fieldset>
</form>
</div>
</div>
---- jquery code (working)----
var mouse_is_inside = false;
$(document).ready(function() {
$(".action").click(function() {
var loginBox = $("#dashboard_box");
if (loginBox.is(":visible"))
loginBox.fadeOut("fast");
else
loginBox.fadeIn("fast");
return false;
});
$("#dashboard_box").hover(function(){
mouse_is_inside=true;
}, function(){
mouse_is_inside=false;
});
$("body").click(function(){
if(! mouse_is_inside) $("#dashboard_box").fadeOut("fast");
});
});
You probably want to use ajax to load the content from the server. Take a look at jquery's .load() method: http://api.jquery.com/load/
You could include a data attribute per link:
<a class="action" data-content="content.php?method=load_content1"></a>
<a class="action" data-content="content.php?method=load_content2"></a>
js would look something like this:
$(".action").on('click', function() {
$("#dashboard_box fieldset").load($(this).data('content'), function() {
var loginBox = $("#dashboard_box");
if (loginBox.is(":visible"))
loginBox.fadeOut("fast");
else
loginBox.fadeIn("fast");
}
return false;
});
Then, in your content.php file you could check the method parameter in the url to determine what content to return.
My php is a little rusty, but something like this:
<?
call_user_func($_GET['method']); // i'm not sure how safe this is. you may want to be more explicit
?>
You can just add data attribute in each of your link's
<a href="#" data-url="content1.php" ..
Then on click of any of the a you can get the php to be called.
$('a').on('click',function(){
var phpFunctionToCall = $(this).data('url');
});
You probably need to make ajax call to load content into your fieldset As this <? load_content1();?> run's on server and javascript have no control over it.
Thanks for all the help. this is what I ended up doing.
--- HTML ---
<a class="action" data-content="content.php?method=load_content2"></a>
---Jquery---
var mouse_is_inside = false;
$(document).ready(function() {
$(".action").click(function() {
var phpFunctionToCall = $(this).data('content');
$('#indhold').load(phpFunctionToCall);
var loginBox = $("#dashboard_box");
if (loginBox.is(":visible"))
loginBox.fadeOut("fast");
else
loginBox.fadeIn("fast");
return false;
});
$("#dashboard_box").hover(function(){
mouse_is_inside=true;
}, function(){
mouse_is_inside=false;
});
$("body").click(function(){
if(! mouse_is_inside) $("#dashboard_box").fadeOut("fast");
});
});
--- PHP (shortened)---
function load_settings_panel($settingOnRequest) {
return $settingOnRequest;
}
$result = call_user_func('load_settings_panel', $_GET['method']);
echo($result);
I know very well that this question is ask many time but some how my problem is different and i didn't find the solution.
Now comes on issue.
I have a an image,so whenever i click on to the image the pop up window is open and show the data.now i want to make this dynamic and wants that the data should be come according to the id.
here is my image code.:
<div id='contact-form'>
</div>
<!-- preload the images -->
<div style='display:none'>
<img src='<?php echo base_url();?>assets/images/img/contact/loading.gif' alt='' />
</div>
And on its click the ajax is called from js file here is the code of that js file:
//here the provider is controller and show_coupon_pre is a function which call the view having the data.
$.get("provider/show_coupon_pre", function(data){
// create a modal dialog with the data
$(data).modal({
closeHTML: "<a href='#' title='Close' class='modal-close'>x</a>",
position: ["15%",],
overlayId: 'contact-overlay',
containerId: 'contact-container',
onOpen: contact.open,
onShow: contact.show,
onClose: contact.close
});
});
I have a table having no of records and each record having this type of image.where the user can click and get the preview in pop window.
But i want to show the data in pop up according to the row like for id=1 it should be show the data of id==1 and so on. now can you you guys please help me how can i pass this id to the js file by which i can fetch the required data.
I am working in codeignitor.
Simplest solution
<table>
<tr>
<td><img class="banner" src="/image1.png" post_id="1"></td>
<td>some other data 1</td>
</tr>
<tr>
<td>
<td><img class="banner" src="/image1.png" post_id="2"></td>
<td>some other data 2</td>
</tr>
</table>
Javascript:
$('.banner').click(function() {
post_id = $(this).attr("post_id");
// send your ajax with post_id
} );
If you have the ID in php when outputting the image, I would do this:
<script>
var randomId = <?php echo '"' . $phpId . '"'; ?>
</script>
I'm using twitter bootstrap's modal.
I have a table that maps mac addresses to vendor names. The code looks something like this
<tbody>
<?php foreach ($rowarr as $k => $v) { ?>
<tr>
<td><?php echo $k ?></td>
<td>
<div class="divBox">
<a data-toggle="modal" href="#myModal"><?php echo $v; ?></a>
</div>
</td>
</tr>
<?php } ?>
</tbody>
I would like to click on the vendor name (like Cisco, HP) and launch a modal dialog box with more details about the vendor. The details in this modal box would come from a database. I want to use PHP to query the database, but for querying I need to know the name of the link/vendor that was clicked. How can I do so ?
My JS for launching the modal looks like this atm
<script type="text/javascript">
$('#myModal').modal('hide');
</script>
First thing you can do is set the data-vendor in the link by doing:
<a data-toggle="modal" data-vendor="<?= $v ?>" href="#myModal"><?php echo $v; ?></a>
Or you could get the vendor by:
vendor = $("#myModal").html();
Then wire up your click event for the link by doing:
<script type="text/javascript">
$('#myModal').click(function(){
$(this).modal('hide');
var vendor = $(this).data('vendor');
//DO SOME AJAX CALL OR REFRESH THE PAGE USING "vendor"
});
</script>
I also noticed that you are trying to select using by the id $('#myModal'), but there isn't an "id" attribute. You would probably want a class called "myModal" on the "a" tag and then select by using:
$('.myModal').click(function(){ ... });
I have a list of products from a particular supplier being displayed in a <table> populated with data from my mysql database.
Within this table, each product has a link that, when clicked, should show the product details in a ui-dialog, <div id = "detalhe_produto"></div>. The dialog does open, however, it only works only for the first item in the list. The other links of the following lines do not open the dialog.
Can anyone help me?
Here's the script I am using to open the ui-dialog:
<script type="text/javascript">
$(function(){
$('detalhe_produto').click(function(){
var selectedVal=$("#detalhe_produto").val();
var url="produto_detalhe.php?codigo="+selectedVal;
var dialog = $("#detalhe_produto");
if ($("#detalhe_produto").length == 0) {
dialog = $('<div id="detalhe_produto" style="display:hidden"></div>').appendTo('body');
}
dialog.load(
url,
{},
function (responseText, textStatus, XMLHttpRequest) {
dialog.dialog({
close: function (event, ui) {
dialog.remove();
},
modal: true,
width: 460
});
}
);
});
});
</script>
and then the code table:
<table border="0" cellpadding="0" cellspacing="3">
<?php do { ?>
<tr>
<td align="left" valign="top" class="lista_produto"><?php echo $row_lista_produtos['codigo']; ?></td>
<td width="15"></td>
</tr>
<?php } while ($row_lista_produtos = mysql_fetch_assoc($lista_produtos)); ?>
</table>
I have tried to change the href="#" to href="javascript: void (0)"
and the result was the same.
Suggestions?
If you have many distinct dialogs that can be clicked open, you need to use a class selector, . instead of an ID selector, #. You may have also forgotten the #.
So instead of this:
var dialog = $("#detalhe_produto");
do this:
var dialog = $(".detalhe_produto");
**Please see this jsfiddle: http://jsfiddle.net/trpeters1/uFDdb/2/
It has a complete working demonstration of the jqueryUI dialog for multiple dialogs specific to your use case. Meaning, the dialog shows a value specific to which link was clicked.
you missed a # in $('detalhe_produto').click(function(){