Get MySQL data inside jQuery UI Dialog - php

I have list of user and I want to open each user detail in jQuery UI Popup. I write whole code but I'm confused how to pass each user ID to the dialog. Below is my code,
HTML
<div id="dialog" title="User Detail">
<p><?php $userID = $_GET[userID]; ?></p>
</div>
<a id="opener" href="#?userID=1">User 1</a>
<a id="opener" href="#?userID=2">User 2</a>
jQuery
jQuery.noConflict();
jQuery(document).ready(function(){
jQuery("#dialog").dialog({
modal: true,
autoOpen: false,
resizable: false
});
jQuery( "#opener" ).click(function() {
jQuery( "#dialog" ).dialog( "open" );
});
});

To start, you cannot reuse an ID for a DOM element, so the selector you're using will only work for the first instance of the link with id="opener".
When you open the dialog, jQuery is simply showing an element on the current page which already exists and is not making a request to that or a new page. $_GET is a server-side super global and is only displayed when the page loads.
What you need to do is:
use "opener" as a class instead of an id
override the dialog's open
function to accept an additional parameter append that parameter to
your dialog markup
You can accomplish this with the following code which grabs the user id from the href attribute:
HTML:
<div id="dialog" title="User Detail">
<p></p>
</div>
<a class="opener" href="#?userID=1">User 1</a>
<a class="opener" href="#?userID=2">User 2</a>
Javascript:
jQuery.noConflict();
jQuery(document).ready(function(){
jQuery("#dialog").dialog({
modal: true,
autoOpen: false,
resizable: false,
open: function (event, ui) {
var id = $(this).data('id');
$('#dialog p').text(id);
}
});
jQuery( ".opener" ).click(function(event) {
event.preventDefault();
var id = $(this).attr('href').split('=')[1]; // grab id
jQuery( "#dialog" ).data('id', id).dialog( "open" );
});
});
The PHP to generate each link would look something like this:
$users = array(
array(
'id' => 1,
'name' => 'User 1',
),
array(
'id' => 2,
'name' => 'User 2',
)
);
foreach ($users as $user) {
print "<a class=\"opener\" href=\"#?userID=$user[id]\">$user[name]</a>";
}
*note I was lazy on that last one and escaped the quotes within the printed string

For a start IDs must be unique so I changed your opener ID into a class.
Then you may want to look at this:
jQuery.noConflict();
jQuery(document).ready(function(){
jQuery("#dialog").dialog({
modal: true,
autoOpen: false,
resizable: false
});
jQuery( ".opener" ).click(function() {
jQuery("#dialog p").text(jQuery(this).attr("href").substring(jQuery(this).attr("href").indexOf('=') +1));
jQuery( "#dialog" ).dialog( "open" );
});
});
In short, you build your dialog and then you display it.
And the working Fiddle

Related

dialog in jquery ui won't open

I'm trying to open a dialog, please see comment below:
var user_ids = $('.user_ids:checked').map(function() {
return this.value;
}).get();
var opt = {
autoOpen: false,
modal: true,
width: 550,
height:650,
title: 'Dialog'
};
tried this one but
this wont trigger open dialog
$("#dialog-confirm")
.dialog(opt)
.data('uids','user_ids')
.dialog({
open: function( event, ui ) {
alert('do something!');
},buttons:{
'Confirm': function() {
//do something
$(this).dialog('close');
},
'Cancel': function() {
$(this).dialog('close');
}
}
});
this one successfully open the dialog box
but i want to put the Confirm and Cancel button
$("#dialog-confirm")
.dialog(opt)
.data('uids','user_ids')
.dialog('open');
});
how can i open the dialog with button triggers?
According to the jQuery UI documentation: http://api.jqueryui.com/dialog/#option-buttonsThis is how you put buttons inside your dialog
$(document).ready(function(){
var user_ids = $('.user_ids:checked').map(function() {
return this.value;
}).get();
var opt = {
autoOpen: true,
modal: true,
width: 300,
height:300,
title: 'Dialog'
};
$( ".selector" )
.dialog(opt)
.data('uids','user_ids')
.dialog({
buttons: [
{
text: "Confirm",
icon: "ui-icon-heart",
click: function() {
console.log("confirmed");
}
},
{
text: "Cancel",
icon: "ui-icon-heart",
click: function() {
$( this ).dialog( "close" );
}
}
]
});
$( ".selector" ).dialog( "open" );
});
<script
src="https://code.jquery.com/jquery-3.3.1.min.js"
integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="
crossorigin="anonymous"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.css">
<script
src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js"
integrity="sha256-VazP97ZCwtekAsvgPBSUwPFKdrwD3unUfSGVYrahUqU="
crossorigin="anonymous"></script>
<body>
<div class="selector" title="Basic dialog">
<p>This is the default dialog which is useful for displaying information. The dialog window can be moved, resized and closed with the 'x' icon.</p>
</div>
</body>

php loop pop up box and dialog from jquery

Hi I am using a framework called CodeIgniter and its a sample of MVC. I need a popup box where it will display the subvalues of an application however my dialog box is not working everytime I echo it. but you see i need to loop the dialog box for different values. my code is below
<?php
foreach($people as $row){
echo"<div id='dialog' title='Basic dialog'>".$row->app_name."</div>";
echo "<button id='opener'>".$row->app_name."</button>";}
?>
My javascrip code is fro jquery
<script>
$(function() {
$( "#dialog" ).dialog({
autoOpen: false,
show: {
effect: "blind",
duration: 1000
},
hide: {
effect: "explode",
duration: 1000
}
});
$( "#opener" ).click(function() {
$( "#dialog" ).dialog( "open" );
});
});
</script>
Use event delegation
$(document).on("click" , "#opener" ,function() {
$( "#dialog" ).dialog( "open" );
});
Update the code as below:
<?php
$i = 1;
foreach($people as $row){
echo"<div id='dialog". $i."' title='Basic dialog'>".$row->app_name."</div>";
echo "<button id='opener". $i."'>".$row->app_name."</button>";
?>
<script>
$(function() {
$( "#dialog<?php echo $i?>" ).dialog({
autoOpen: false,
show: {
effect: "blind",
duration: 1000
},
hide: {
effect: "explode",
duration: 1000
}
});
$( "#opener<?php echo $i?>" ).click(function() {
$( "#dialog<?php echo $i?>" ).dialog( "open" );
});
});
</script>
<?php
$i++;
}
?>
Basically there will be multiple popup and you need to give unique id to each popup and button.
to use id in this situation is not good idea ... you should change id to class
echo "<button class='opener'>".$row->app_name."</button>";
then
$( ".opener" ).click(function() {
$( "#dialog" ).dialog( "open" );
});
id is unique you should use a class
Enjoy :)

Content of PHP won't show in jQuery dialog

I'm trying to load the content of a PHP file in a jQuery UI dialog, but the dialog won't open.
If I debug the code with FireBug, there seems to be an break without any error report on the following line $('#formDialog_open').load($(this).attr('href'), function()
HTML
<div id="formDialog_open" class="widget grid6" title="Dialog with form elements">
//my php codes
</div>
hyperlink what fires the dialogbox
<a href="edit.php?id=' .$aRow['id']. '" id="form" class="tablectrl_small bDefault tipS" title="Edit">
The Javascript
$('#form').live('click',function(e) {
e.preventDefault();
$('#formDialog_open').load($(this).attr('href'), function(){
$('#formDialog_open').dialog({
title: 'User Administration',
resizable: true,
modal: true,
hide: 'fade',
width:350,
height:275,
});//end dialog
});
});
Suggest you following `
$("#button1").click(function () {
$('#formDialog_open').dialog({
title: 'User Administration',
resizable: true,
modal: true,
hide: 'fade',
width:350,
height:275,
});//end dialog
});`
Check it out and find out that it seems click event is not execute. So I have given a code for the #button1 click event.
Also you can use followJquery Dialog and Click Event Jquery documents

full calendar + jquery ui dialogue

I am using a fullcalendar:-
http://arshaw.com/fullcalendar/
and I am using jquery .post to get a parameter back to the same page to generate some result, and this is working well.
At the same time, I wish to use jquery ui dialogue to hold the displayed contents. While pasting the sample codes from official site, the example works. However, when combining the .post output with dialogue, it was not successful.
I would like to seek help in combining the below 2 sets of scripts:-
//for generating .post output (working!)
<script>
function event_details(thevalue){
$.post('module/calendar/event_details.php',{
eid:thevalue},
function(output){
$('#theeventmsg').html(output);
});
}
</script>
<div id='theeventmsg'></div>
//jquery ui dialogue (working!)
<script>
// increase the default animation speed to exaggerate the effect
$.fx.speeds._default = 1000;
$(function() {
$( "#dialog" ).dialog({
autoOpen: true,
show: "blind",
hide: "explode"
});
$( "#opener" ).click(function() {
$( "#dialog" ).dialog( "open" );
return false;
});
});
</script>
<div class="demo">
<div id="dialog" title="Basic dialog">
<p>This is an animated dialog which is useful for displaying information. The dialog window can be moved, resized and closed with the 'x' icon.</p>
</div>
<button id="opener">Open Dialog</button>
</div><!-- End demo -->
Can help??? Thanks a lot!!
Try something like this:
<script>
$.fx.speeds._default = 1000;
$(document).ready(function() {
$( "#dialog" ).dialog({ autoOpen: false });
$('#button').click(function () {
var data = { ... };
$.post('module/calendar/event_details.php', data, function (output) {
$('#dialog p').html(output);
$( "#dialog" ).dialog("open");
});
});
});
</script>
<div id="dialog">
<p>content</p>
</div>
<button id="button">button</button>
Or:
<script>
$(document).ready(function () {
function eventdetail(thevalue) {
$.post('event_details.php', { eid: thevalue }, function (output) {
$('#dialog p').html(output);
$("#dialog").dialog({ autoOpen: true });
});
}
$('#button').click(function () { eventdetail('value'); });
});
</script>
<div id="dialog">
<p>content</p>
</div>
<button id="button">button</button>

Showing/hiding the next div on mouseover of the previous div

This is my php code:
echo '<div class="code" rel="$url">$title</div>
<div class="tip">Copy and open site</div>';
the above is a loop's echo result. Namely, there maybe 0->n like the following html structure
<div class="code" rel="....">...</div>
<div class="tip">Copy and open site</div>
Now, supposed there are 6 results like the above, I want to use jQuery to get when I put the mouse hover on the first <div class="code" rel="....">...</div> the only first tip div will show. When I move the mouse out, the div will be hidden.
I think using jQuery alone can't get that effect. There must be some php added to the code, but I don't know how to do that?
$( 'div.code' ).mouseover( function() {
$( this ).next( 'div.tip' ).show();
} );
$( 'div.code' ).mouseout( function() {
$( this ).next( 'div.tip' ).hide();
} );
Edit based on your first comment:
There's a few possibilities:
$( document ).ready( function() { // added document ready for completeness
$( 'div.code' ).mouseover( function() {
$( this ).next( 'div.tip' ).show();
} );
$( 'div.code' ).mouseout( function() {
$( this ).next( 'div.tip' ).hide();
} );
// this is a way after declaring previous stuff:
$( 'div.code' ).trigger( 'mouseout' );
// or simply do:
$( 'div.tip' ).hide();
} );
You'll need to refer to the tip DIV more specifically than just a class selector.
Something like...
<div id="link1" class="code" rel="....">...</div>
<div id="tip1" class="tip">Copy and open site</div>
And then have the hover property of #link1 point to your JQuery display code.

Categories