Get ajax jquery to reset when i click on another link - php

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

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

submitHandler: triggering incorrect dialog

I am trying to debug a problem I have with submitHandler: function. What is happening, in firebug it is displaying the correct error message based on the true handle in the submitHandler. But, it is firing the success dialog and not the error dialog. I am fairly new to jquery and ajax so I have proably got some brackets or commas in the wrong place.
I would be grateful if someone could point out my error. Thanks
submitHandler: function() {
if ($("#USRboxint").valid() === true) {
var data = $("#USRboxint").serialize() + '&submit=true';
$.post('/domain/users/box.php', data, function(msgs) {
if (msgs.boxerror === true) {
var $dialog = $('<div id="dialog"></div>').html('<br /><b>Your New entry was NOT SUBMITTED.<br /><br />Thank you.</b>');
$dialog.dialog({
autoOpen: true,
modal: true,
title: 'New entry Unsuccessfull',
width: 400,
height: 200,
draggable: false,
resizable: false,
buttons: {
Close: function() {
$( this ).dialog( "close" );
}
}
});
}
else
var messageOutputs = '';
for (var i = 0; i<msgs.length; i++){
messageOutputs += msgs[i].box+' ';
}
$("#USRboxint").get(0).reset();
var $dialog = $('<div id="dialog"></div>').html('<br /><b>Your New entry was successfully submitted.<br /><br />Thank you.</b>');
$dialog.dialog({
autoOpen: true,
modal: true,
title: 'New entry successfull',
width: 400,
height: 200,
draggable: false,
resizable: false,
buttons: {
Close: function() {
$( this ).dialog( "close" );
}
}
});
//$("#frmreport").get(0).reset();
}, 'json');
}
},
success: function(msgs) {
}
PHP Code
<?php
$boxerrortext = 'No duplicates';
$array = split('[,]', $_POST['box_add']);
if (isset($_POST['submit'])) {
if ($box == 'DEMO111')
{
$error = array('boxerror'=>$boxerrortext);
$output = json_encode($error);
echo $output;
return;
}
else
$form = array();
foreach ($array as $box) {
// if (empty($box)) {
// $error = array('boxerrortext'=>$boxerrortext);
// $output = json_encode($error);
// echo $output;
// }
// else
// {
$form[] = array('dept'=>$dept,
'company'=>$company,
'address'=>$address,
'service'=>$service,
'box'=>$box,
'destroydate'=>$destroydate,
'authorised'=>$authorised,
'submit'=>$submit);
}
}
$result=json_encode($form);
echo $result;
?>
Creating dialogs each time is not efficient, better to initialize the dialogs on document ready:
$(function(){
$('<div id="dialog_error"></div>').html('<br /><b>Your New entry was NOT SUBMITTED.<br /><br />Thank you.</b>').dialog({
autoOpen: false,
modal: true,
title: 'New entry Unsuccessfull',
width: 400,
height: 200,
draggable: false,
resizable: false,
buttons: {
Close: function () {
$(this).dialog("close");
}
}
});
$('<div id="dialog_success"></div>').html('<br /><b>Your New entry was successfully submitted.<br /><br />Thank you.</b>').dialog({
autoOpen: false,
modal: true,
title: 'New entry successfull',
width: 400,
height: 200,
draggable: false,
resizable: false,
buttons: {
Close: function () {
$(this).dialog("close");
}
}
});
});
Then open them when you need them:
submitHandler: function () {
if ($("#USRboxint").valid() === true) {
var data = $("#USRboxint").serialize() + '&submit=true';
$.post('/domain/users/box.php', data, function (msgs) {
if (msgs.boxerror === true) {
$("#dialog_error").dialog("open");//Open error dialog
} else var messageOutputs = '';
for (var i = 0; i < msgs.length; i++) {
messageOutputs += msgs[i].box + ' ';
}
$("#USRboxint").get(0).reset();
$("#dialog_success").dialog("open");//Open success dialog
//$("#frmreport").get(0).reset();
}, 'json');
}
}
You should add braces around the code for the else-statement, or else only the first line is is part of the else block, and all the lines after it are executed no matter what.
Change this:
else
var messageOutputs = '';
To this:
else {
var messageOutputs = '';
// Other lines that belong in the else block.
}
I disagree with Wilmer, but you should properly clean up the dialog when it is closed. Since you create the div just to display the dialog once, you should remove it when the dialog is closed. You can do this by adding the following dialog option:
close: function() { $(this).dialog('destroy').remove(); },

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

Preview with PHP value in dialog jquery

I created a div for preview with PHP and now I need to display it in Dialog Jquery,is it possible to pass directly php variable in Jquery?I tried but I have as results a blank dialog!!
Jquery side :
$(function() {
$( "#previewDiv" ).dialog({
autoOpen: false,
height: 200,
width: 700,
modal: true,
buttons: {
"Submit": function() {
$( this ).dialog( "close" );
$("#submit").click();
},
"Cancel": function() {
$( this ).dialog( "close" );
}
},
close: function() {
$('#').val("");
}
});
$( "#previewButton" ).click(function() { $( "#previewDiv" ).dialog( "open" ); });
});
And PHP :
$table=$_SESSION['data'];
if (isset($_POST['preview'])) {
if ($table) {
foreach($table as $keys=>$values) {
$pieces = explode(" , ",$values);
echo "<div id='previewDiv'>"
echo "$pieces[0]";
echo "$pieces[1]";
echo "</div>";
}
}
Thank you for your help!!!

load another php page dynamically in jquery ui on click of a button

jQuery(function ($) {
// Load dialog on page load
// Load dialog on click
$('#basic-modal .basic').click({
modal:true,
open: function ()
{
var id = $('#id').attr('value');
$(this).load('edit.php?id='+id);
},
height: 400,
width: 400,
title: 'Dynamically Loaded Page'
});
});
This is not opening the dialog on the click of the button. what is it that i am doing wrong here?
You are passing options from jQuery UI .dialog() to .click(). Try this:
jQuery(function ($) {
// Load dialog on page load
// Load dialog on click
$('#basic-modal .basic').click(function(){
$('<div id="dialogContainer"></div>').appendTo('body').dialog({
modal:true,
open: function ()
{
var id = $('#id').attr('value');
$(this).load('edit.php?id='+id);
},
height: 400,
width: 400,
title: 'Dynamically Loaded Page'
});
});
});
try doing things in reverse
function getEdit(id)
{
$.get("edit.php", {id: id}
function(data)
{
$("#div-id").html($("#div-id").html() + data)
$("#div-id").dialog({
modal:true,
height: 400,
width: 400,
title: 'Dynamically Loaded Page'
});
},
"html");
}
getEdit(); // you can also set up a button to call this as well.

Categories