Calling a JQuery UI Dialog function from PHP - php

I am quite new to the JQuery language but I have found a tutorial on the net that helped me to get the dialog working from a click button.
the function i use is as following:
$("#registration_ok").click(function() {
$("#dialog").attr('title', 'Registration').text('Your Registration was Successfull!').dialog({ buttons: { 'Ok': function() {
$(this).dialog('close');
}}, open: function(){
jQuery('.ui-widget-overlay').bind('click',function(){
jQuery('#dialog').dialog('close');
})
}, closeOnEscape: true, draggable: false, resizable: false, hide: 'fade', show: 'fade', modal: true, dialogClass: 'success'});
});
I now want to use this function and call it from a php file, but i cant get it to work.
I might need to rewrite the function since i think it will only respond on an actual click so i tried the following:
function dialog() {
$("#dialog").attr('title', 'Registration').text('Your Registration was Successfull!').dialog({ buttons: { 'Ok': function() {
$(this).dialog('close');
}}, open: function(){
jQuery('.ui-widget-overlay').bind('click',function(){
jQuery('#dialog').dialog('close');
})
}, closeOnEscape: true, draggable: false, resizable: false, hide: 'fade', show: 'fade', modal: true, dialogClass: 'success'});
};
I have tried to call the function in different ways, but none did work:
if(isset($_GET['success'])) { echo '<script type="text/javascript"> function() { dialog(); } </script>'; }
Or
if(isset($_GET['success'])) { echo '<script type="text/javascript"> dialog();</script>'; }
none did work, can anyone tell me what i am doing wrong?

Have you tried wrapping the code to be fired in a .ready(); function?
$(document).ready(function(){
});
This will ensure the jquery only fires when the page has loaded. I suspect it's firing too early before all of the elements are present thus not working correctly.
The new code would look something like this:
<?php
if(isset($_GET['success'])){
echo '<script type="text/javascript">
$(document).ready(function(){
dialog();
});
</script>';
}
?>

Related

jquery dialog won't open again after closing

I cannot find an answer in previous posts so I'll share my code and hopefully someone can tell me why my dialog won't open after I have closed it the first time.
script:
addressdialog = $( "#address-form" ).dialog({
autoOpen: false,
height: 550,
width: 450,
modal: true,
buttons: {
Submit: function(){
form[0].submit();
},
Cancel: function() {
addressdialog.dialog( "close" );
}
},
close: function() {
form[ 0 ].reset();
allFields.removeClass( "ui-state-error" );
}
});
$( "#create-address" ).click( function() {
addressdialog.dialog( "open" );
});
I can't get my html div to display.... let me know if it is required but I figure the problem is with the script.

Get ajax jquery to reset when i click on another link

index.php
$(document).ready(function(){
$('a.bruker').click(function() {
var idx = $(this).attr('id');
$.ajax({
url:'bruker.php',
type:'POST',
data: 'id='+idx,
cache: false,
success:function(data) {
$('#bruker').html(data);
}
});
});
});
echo "<div id='bruker'></div>";
<a class='bruker' id='1'>name1</a>
<a class='bruker' id='2'>name2</a>
bruker.php
if(isset($_POST['id'])) {
echo "<script> $(function() {
$('.bruker-box').dialog({
autoOpen:true,
resizable: false,
closeOnEscape: true,
width:600,
height:500,
show: 'fade',
modal: true,
open: function(event, ui) { },
position: { my: 'center', at: 'center' }
})
$('#ui-id-2').css('border', 'none');
$('.ui-widget-overlay').click (function () {
$('.bruker-box').dialog( 'close' );
});
});</script>";
echo "<div class='bruker-box' title='(".$_POST['id'].")'>";
echo "example";
echo "</div>";
When i click out of the first box (name1) and click on name2 i want to get only the name 2 to come up, but the first box (name1) comming up first and than name2.
How can i get this right? Is there a way to reset somthing when i click on a new one?
This is happening because, every time an anchor is clicked, you are injecting a new div containing a dialog set to autoOpen. (Turn on Developer Tools F12 in Chrome and watch what happens.)
The autoOpen divs accumulate, redisplaying themselves with each change to the DOM.
To solve this, you can remove() the div after it displays. Change your bruker.php file to look like this:
Note the addition of a close: function in the dialog definition:
PHP:
if(isset($_POST['id'])) {
$out = "
<script>
$(function() {
$('.bruker-box').dialog({
autoOpen:true,
resizable: false,
closeOnEscape: true,
width:600,
height:500,
show: 'fade',
modal: true,
open: function(event, ui) { },
position: { my: 'center', at: 'center' },
close: function() {
$('div.ui-dialog').remove();
}
}); //END .dialog()
$('#ui-id-2').css('border', 'none');
$('.ui-widget-overlay').click (function () {
$('.bruker-box').dialog( 'close' );
});
}); //END document.ready
</script>";
$out .= "<div class='bruker-box' title='Name: (" .$_POST['id']. ")'>";
$out .= "example";
$out .= "</div>";
echo $out;
}

jquery ui confirmation dialog not working

I have a series of checkboxes that correspond to entries, and a delete all button that I have attached a jquery ui modal confirm. Fiddle here: http://jsfiddle.net/BxdWc/. The issue is that, it does not submit the form (is not processed by php) after hitting yes in the confirmation dialog. The php code is include at the top of the page, but I am fairly certain it has nothing to do with this, as without the modal the code processes, and deletes the checked entries properly.
JS:
$(function () {
$("#dialog-confirm-multiple").dialog({
autoOpen: false,
resizable: false,
width: 300,
modal: true,
show: {
effect: "bounce",
duration: 100
},
hide: "drop",
buttons: {
"Yes": function () {
$("#confirm").submit();
$(this).dialog("close");
},
"No": function () {
$(this).dialog("close");
}
}
});
$("#doDelete").click(function (e) {
e.preventDefault();
$("#dialog-confirm-multiple").dialog('open');
return false;
});
});
HTML:
<form post="self.php" id="confirm">
<!-- some inputs .etc -->
<input name="doDelete" type="submit" id="doDelete" value="Delete" class="btn btn-danger">
</form>
It looks like your PHP is checking for which button is clicked. jQuery does not include the buttons when you submit a form via $('#confirm').submit();. The browser will only include the buttons in the submission if the button was actually clicked.
There are a couple ways to get the button name/value pair to be included in the submission.
You can use $('form').serializeArray(); and push the doDelete/Delete pair as described in this answer
Or you might be able to get away with doing something like this. Notice the click of the delete button is called a second time. The button click event checks if it was initiated via a trigger (aka the dialog). The second time around, it will not be prevented from being submitted.
$(function () {
$("#dialog-confirm-multiple").dialog({
autoOpen: false,
resizable: false,
width: 300,
modal: true,
show: {
effect: "bounce",
duration: 100
},
hide: "drop",
buttons: {
"Yes": function () {
$(this).dialog("close");
$("#doDelete").click();
},
"No": function () {
$(this).dialog("close");
}
}
});
$("#doDelete").click(function (e,ui) {
if(e.originalEvent) {
e.preventDefault();
$("#dialog-confirm-multiple").dialog('open');
return false;
}
});
});
The fiddle is here
I modified this a little bit so that it can work when you denote the confirm button with a class rather than an id. I'm a javascript novice so beware! Feel free to improve on this.
$(function () {
$("input.doDelete").click(function (e,ui) {
var button = $(e.target);
$("#dialog-confirm-multiple").dialog({
autoOpen: false,
resizable: false,
width: 300,
modal: true,
show: {
effect: "bounce",
duration: 100
},
hide: "drop",
buttons: {
"Yes": function () {
$(this).dialog("close");
button.click();
},
"No": function () {
$(this).dialog("close");
}
}
});
if(e.originalEvent) {
e.preventDefault();
$("#dialog-confirm-multiple").dialog('open');
return false;
}
});
});

How can i callback from infinity scroll?

What's the correct way to callback from infinity scroll? currently this `
$(function() {
code();
$('#catview').infinitescroll({
dataType: 'html',
navSelector: 'div.nextPage',
nextSelector: 'div.nextPage a:first',
itemSelector: '#catalogue',
loading: {
finishedMsg: '',
msgText: '',
img: '/../../../images/loading.gif',
},
debug: true,
animate: true
}, $(function() {
code();
}));
});`
Doens't work :( if i set an alert before code() i will see it when page loads and that's it... the other part of the code is :
function code() {
alert('1')
$('.teest').caroufredsel({
direction: 'left',
circular: true,
items: {
visible: 1,
minimum: 2,
},
scroll: {
fx: 'fade',
easing: 'easeOutCubic',
duration: 200,
},
auto: {
duration: 1600
},
width: 208,
height: 265,
prev: {
button: '.prev'
},
next: {
button: '.next'
}
});
}
all i see if run those 2 codes is the alert 1 , the caroufredsel working on the first page, if i scroll down to seconds page nothing happens, it just loads
Please try this :-
$(function() {
$('#catview').infinitescroll({
dataType: 'html',
navSelector: 'div.nextPage',
nextSelector: 'div.nextPage a:first',
itemSelector: '#catalogue',
loading: {
finishedMsg: '',
msgText: '',
img: '/../../../images/loading.gif',
},
debug: true,
animate: true
},
function(arrayOfNewElems)
{
code();
});
});
As per Infinite Scroll Jquery documentation call back function change your code
$(function() {
code();
}));
to
function(arrayOfNewElems){
//here your collaback function
});
Turns out $(function... is readed as object from the javascript engine , and infinity-scroll cannot callback objects. Removing all the $ from the function solve everything

How to return user input from jQuery UI dialog box

I want to open a jQuery UI dialog box and the box will have an input field which will take username. I want that when user presses the OK key on the dialog box, the dialog should return the username back to jQuery somehow
$("#dialog").dialog({
autoOpen: false,
width: 600,
buttons: {
"Ok": function() {
$(this).dialog("close");}}
});
A dialog is asynchronous, which makes it meaningless to "return" something.
You can, however, use one of the callback functions to perform actions with the user input.
For example, using the 'OK' button callback:
$("#dialog")
.append(
$("<input>")
.attr({
"type" : "text",
"id" : "user-input"
})
)
.dialog({
autoOpen: false,
width: 600,
buttons: {
"Ok": function() {
alert( $("#user-input").val() );
$(this).dialog("close");
}}
});
If you just have a div on your page with ID dialog that contains the input field, just select it using a regular jQuery selector when the user presses OK.
Try this on "OK" funcion
var myvalue= $("#yourinputtext").val();
alert(myvalue);
You can capture the value in the ok button click handler, and then do with it as your require:
$("#dialog").dialog({
autoOpen: false,
width: 600,
buttons: {
"Ok": function() {
var inputValue = $("#myInput").val();
// do stuff with the value...
$(this).dialog("close");}
}
}
);

Categories