jquery ui confirmation dialog not working - php

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

Related

Calling a JQuery UI Dialog function from 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>';
}
?>

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

How to submit a form using jquery dialog button?

Hello guys just want to ask about how can i process a form submission using the jquery dialog button. In my code I have a button that when you click. It will pop up a form in a dialog box. Below is the jquery buttons for OK and CANCEL. My problem is I can't submit my form the only option that I have is to create a submit button inside my form. But i want to use the jquery button instead. Im using CodeIgniter I hope you can help me.
My view (showAllSuppliers.php)
/* FOR ADD PAGE */
$(".hero-unit input[name=add_supplier]").on('click',function(){
$('#popup').load("<?php echo site_url("supplier_controller/addNewSupplier/"); ?>").dialog({
title: "Add New Supplier",
autoOpen: true,
width: 800,
modal:true,
position: "center",
buttons: {
OK: function(){
$("#addSupplier").submit(); //HOW CAN I SUBMIT MY FORM USING THIS?
},
CANCEL: function() {
$(this).dialog( "close" );
}
}
});
});
my form (addNewSupplier.php)
<?php
$attr = array('id'=>'addSupplier');
echo form_open('supplier_controller/insertNewSupplier');
..
..
..
... MY TEXTBOX FIELDS HERE
..
..
//echo "<input type='submit' value='ADD' />"; ANOTHER OPTION FOR SUBMISSION
echo form_close();
?>
my controller function(supplier_controller.php)
public function insertNewSupplier(){
$this->supplier_model->insertNewSupplierDetail();
redirect('supplier_controller/index','refresh');
}
Try by just changing this line and your usual js:
$attr = array('id'=>'addSupplier');
echo form_open('supplier_controller/insertNewSupplier', $attr);
If still does not submits the form then try to submit by ajax call.
$(".hero-unit input[name=add_supplier]").on('click',function(){
$('#popup').load("<?php echo site_url("supplier_controller/addNewSupplier/"); ?>").dialog({
title: "Add New Supplier",
autoOpen: true,
width: 800,
modal:true,
position: "center",
buttons: {
OK: function(){
$.ajax({
url : '<?=base_url()?>supplier_controller/insertNewSupplier',
type : 'POST',
data : $("#addSupplier").serializeArray(),
success : function(resp){
alert("Submitted !!!");
$(this).dialog( "close" );
},
error : function(resp){
//alert(JSON.stringify(resp));
}
});
},
CANCEL: function() {
$(this).dialog( "close" );
}
}
});
});
You need ajax call
$('#submit').click(function (){
var first_name = $('#first_name').val();
$.ajax({
url : '/supplier_controller/insertNewSupplier,
type : 'post',
data : first_name,
success : function(){
//do something;
console.log(first_name);
}
})
return false;
});
That's gonna work if you ad id to submit button and to your input. But you can also call them via html tags. It's just an idea or example for you.
If you want to use classic form submission instead of ajax call, write these code lines for the buttons parameter:
buttons: {
OK: function(){
$(this).dialog().find('form').submit();
},
CANCEL: function() {
$(this).dialog( "close" );
}
}

How to submit the php page from jquery confirmation box?

I created a conformation box from jquery. I want to submit the page and view PHP echo message when click the confirm button in the confirmation box. Can you help me with code that comes for the confirmation button?
My jQuery/javascript function is here:
$(document).ready(function(){
$('#click').click(function(){
//$(function() {
// a workaround for a flaw in the demo system (http://dev.jqueryui.com/ticket/4375), ignore!
$( "#dialog:ui-dialog" ).dialog( "destroy" );
$( "#dialog-confirm" ).dialog({
resizable: false,
height:140,
modal: true,
buttons: {
"Ok": function() {
// I WANT THE CODE FOR HERE TO SUBMIT THE PAGE TO WIEW PHP ECHO MESSAGE
},
Cancel: function() {
$( this ).dialog( "close" );
}
}
});
});
});
You can use jQuery Form submit library. This will give you many options.
And according to your requirement you can call
beforeSubmit: 'YOUR FUNCTION To OPEN DIALOG',
If this returns true your form will get post and you will definitely get response.
see example here : http://malsup.com/jquery/form/#ajaxForm
Use the jQuery Post function...
$.post("test.php", { 'choices[]': ["Jon", "Susan"] });
Here is a version with a callback when the post completed...
$.post("test.php", { name: "John", time: "2pm" },
function(data) {
alert("Data Loaded: " + data);
}
);
Or if you have an existing form that you want to post use the jQuery submit function...
$("form").submit();
But before you do this you need to make sure that you populated the input fields in the form.
try this code,
var parameter_1 = 'test';
var parameter_2 = 'test';
$.ajax(
{
type: "POST",
url: "/submit_url.php",
data: "parameter_1="+ parameter_1 +"& parameter_2 ="+ parameter_2,
success: function(html)
{
alert('Submitted');
}
});

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