jquery: passing parameter to modal - php

I have the following in a table, repeated for each row:
<a <?php echo 'id="'.$id.'"'; ?> class="custom-dialog-btn btn btn-small">
<i class="icon-trash"></i>
</a>
where id is different for each line, because it is taken from a table in database where it is the primary key.
Then, I used th following jQuery code:
$(".custom-dialog-btn").bind("click", function (event) {
$("#mws-jui-dialog").dialog("option", {
modal: false
}).dialog("open");
event.preventDefault();
});
$("#mws-jui-dialog").dialog({
autoOpen: false,
title: "Alert",
modal: true,
width: "640",
buttons: [{
text: "NO",
id: "cancel_button",
click: function () {
$(this).dialog("close");
}
},
{
text: "OK",
id: "confirm_button",
click: function () {
myremovefuntion(id); // I need the id
}}
]
});
that refers to the dialog:
<div id="mws-jui-dialog">
<div class="mws-dialog-inner">
<h2>Are you sure you want to delete?</h2>
</div>
</div>
How can I pass the id to the modal?

You can append a data attribute to your dialog div as follows;
$('a').click(function (e) {
e.preventDefault();
$("#your_dialog").data('mycustomdata', $(this).prop('id')).dialog('open');
});
And retreive it like this
$("#your_dialog").dialog({
autoOpen: false,
resizable: false,
height:200,
modal: true,
buttons: {
Cancel: function() {
$(this).dialog('close');
},
'Delete': function() {
$(this).dialog('close');
var mydata = $(this).data('mycustomdata'); // = gives you the id of anchor element
// some delete logic
}
}
});

While Emin's answer certainly works, I wonder if this fabuolus jQuery UI library perhaps has a dialogue flavour analogue to JavaScript's native confirm() method? Or perhaps a possibility to pass in callback functions? This would remove the need from the dialogue code containing business logic.

Use the following code to get the id:
var id = $(this).prop("id");
And you will get the id of that which element you have clicked.

Related

jquery confirm box not working second page in pagination

I have to show number of users in table in a table with pagination
And I have delete option for each user
Im using datatable pagination
It has to ask confirm before delete the user, So im using jquery .confirm
function (jquer.confirm.js)
This jquery confirm box not working in second page of pagination.
Here is my code:
$(".del_user").each(function(){
var user_id = this.id;
$("#"+user_id).confirm({
text: "Are you sure you want to delete this user?",
title: "Confirmation required",
confirm: function(button) {
<!---delete function()---->
return false;
},
cancel: function(button) {
// nothing to do
},
confirmButton: "Yes",
cancelButton: "No",
post: true,
confirmButtonClass: "btn-danger",
cancelButtonClass: "btn-default"
});
});
How to solve this?
Try this...
your jquery dom not loading for another tab so my experience is doing following like that
".paginate_button" is your tab button class name
$(".paginate_button").click(function(){
$(".del_user").each(function(){
var user_id = this.id;
$("#"+user_id).confirm({
text: "Are you sure you want to delete this user?",
title: "Confirmation required",
confirm: function(button) {
<!---delete function()---->
return false;
},
cancel: function(button) {
// nothing to do
},
confirmButton: "Yes",
cancelButton: "No",
post: true,
confirmButtonClass: "btn-danger",
cancelButtonClass: "btn-default"
});
});
});
You can do this...
add "fnDrawCallback" option in your datatable function:
$('#datable_id').dataTable({
"fnDrawCallback": function () {
createconfirmationbox();
},
});
And your jquery confirmation function look like this :
function createconfirmationbox(){
$('.someclass').confirmation({
onConfirm: function () {
*do something on confirmation*
}
});}

using the form action in jquery to go to php

How can I use the action in html form so I can send all the data form my form
I tried $("#formupload").attr('action') seems not working
Here is my code:
$(".clickupload").click(function () {
$("#dialog-form").dialog("open");
});
$("#dialog-form").dialog({
autoOpen: false,
closeOnEscape: true,
title: "Upload Picture",
width: 400,
height: 300,
modal: true,
buttons: {
Cancel: function () {
$(this).dialog("close");
},
Upload: function () {
$(document).ready(function () {
$("#formupload").attr('action');
});
}
}
});
<div id="dialog-form">
<form id="formupload" action="ProfileImages/FileUpload.php" method="POST" enctype="multipart/form-data">
<input type="file" name="uploadProfilePicture"/>
</form>
</div>
You mean eventually $("#formupload").sumbit();? What you're doing is reading the attribute and then doing nothing with it?
Try simply $("#formupload").submit()
What bweobi said, and eventually put all the jQuery initialisation stuff on document ready to make sure the document is all initialised before you add event handlers to its elements :
(function($) { // Allows you to create variables locally so
// there are no conflict with other stuffs.
$(document).ready(function() {
// You can name jQuery variables with a dollar so you can easily make
// the difference betwen jQuery and non-jquery objects.
$dialogForm = $('#dialog-form'); // Half queries, double speed.
$(".clickupload").click(function () {
$dialogForm.dialog("open");
});
$dialogForm..dialog({
autoOpen: false,
closeOnEscape: true,
title: "Upload Picture",
width: 400,
height: 300,
modal: true,
buttons: {
Cancel: function () {
$(this).dialog("close");
},
Upload: function () {
$("#formupload").submit();
}
}
});
});
})(jQuery); // Alows you not to use $ in global scope in case
// you use multiple libraries that use the $.

jQuery modal user delete option with mysql

Hello I've got another question. I am creating an administration system. I added registration for users and a delete function. Now I need to make a good design.
So I created a table from a MySQL DB with following infos:
ID, Username, Last Login and Delete.
This is an extraction of my php code where I print the table:
echo "<td class=\"center\">
<a href=\"#\" class=\"delete_user\">
<img src=\"images/delete.png\" />
<script type=\"text/javascript\">
var id = \"index.php?section=user_delete&id=".$getUserID[$i]."\";
</script>
</a>
</td>
As you can see I am using the ID for the delete process.
Now I want to use a jQuery modal popup to make sure that I really want to delete this person.
This is my js code:
$(".delete_user").click(function() {
$( "#dialog_delete_user" ).dialog( "open" );
});
$( '#dialog_delete_user' ).dialog({
autoOpen: false,
resizable: false,
height: 170,
modal: true,
buttons: {
'ok': function() {
alert(id);
//document.location = id;
$( this ).dialog( 'close' );
}
}
});
As you can see I need to add a variable id to identify the person and make sure that the right person gets deleted.
I thought that the javascript only executes if i click the link. This seems to be incorrect.
So how can I define/identify each person?
The displayed table is useless cause there is no way to identify each delete button with its owner. So I have to define it right there where I create the table.
Without this jQuery modal form it would be easy. But there has to be a way to get it work. Any idea?
Personally I would set an attribute on the link that opens the dialog something like
Click me!
then in the onclick event of the link that opens the dialog box I would have it set the dialog's hidden user field to the value of $(this).data("user-id"); If you're not doing a form and just immediately firing an ajax request it gets even easier.
var currentUserId;
$(".delete_user").click(function() {
currentUserId = $(this).data("user-id");
$( "#dialog_delete_user" ).dialog( "open" );
});
$( '#dialog_delete_user' ).dialog({
autoOpen: false,
resizable: false,
height: 170,
modal: true,
buttons: {
'ok': function() {
//document.location = "/somephpfile.php?user_id=" + currentUserId;
$( this ).dialog( 'close' );
// ajax version
$.ajax({
url : "/somephpfile.php?user_id=" + currentUserId,
// Other ajax related code.
});
}
}
});

send a php variable in jquery dialog()

I have a table with images and info about each image. Next to each image I have an 'add' button that when clicked opens a jquery dialog() with id="alignment".
I want to send a php variable (with the image name) so when you click on the 'add' button I can call the variable and the image name can be displayed in the dialog box.
My code is:
foreach($uploaded as $upload){
$wgOut->addHTML('<tr><td><button class="imageSetting">Add</button></td><td>');
$wgOut->addHTML($upload[0]);
$wgOut->addHtml('</td><td>');
$wgOut->addHtml('<img src="images/thumb/'.$upload[0].'/120px-'.$upload[0].'" />');
$wgOut->addHtml('</td><td>');
$wgOut->addHTML($upload[1]);
$wgOut->addHTML('x');
$wgOut->addHTML($upload[2]);
$wgOut->addHtml('</td><td>');
$wgOut->addHTML($upload[3]);
$wgOut->addHtml('</td><td>');
$wgOut->addHtml($upload[4]);
$wgOut->addHtml('</td></tr>');
}
$wgOut->addScript('<script type="text/javascript">
( function($) {
$(document).ready(function() {
$(function() {
$( "#alignment" )
.dialog({
autoOpen: false,
title: "Align Image",
//draggable: false,
resizable: false,
buttons: {"Okay": function() {$(this).dialog("close");}},
});
$(".imageSetting")
.click(function() {
$( "#alignment" ).dialog("open");
});
});
});
} ) ( jQuery );
</script>');
$wgOut->addHTML('<div id="alignment">');
$out = self::alignment();
$wgOut->addHtml($out);
$wgOut->addHTML('</div>');
So I added a bit of code to your example, but you can tweak it as you find necessary. Basically I recommend using the jQuery .data() method to pass along the data you need. Each button will utilize the HTML5 data attribute inside the loop to create the image name as part of the button html.
Next, when you click on the button it'll grab that value and assign it to the modal's copy of image-name.
I added an open function to your modal. Inside that function it'll fetch that image-name value (the one we just assigned) and you can now use it to append into the contents of the modal. (For example, you can create a <span> somewhere in your modal html where you'd like to place the image name... And then use the .html() method to "paste" the value in there).
foreach($uploaded as $upload){
$wgOut->addHTML('<tr><td><button data-image-name="' . $upload[0] . '" class="imageSetting">Add</button></td><td>');
$wgOut->addHTML($upload[0]);
$wgOut->addHtml('</td><td>');
$wgOut->addHtml('<img src="images/thumb/'.$upload[0].'/120px-'.$upload[0].'" />');
$wgOut->addHtml('</td><td>');
$wgOut->addHTML($upload[1]);
$wgOut->addHTML('x');
$wgOut->addHTML($upload[2]);
$wgOut->addHtml('</td><td>');
$wgOut->addHTML($upload[3]);
$wgOut->addHtml('</td><td>');
$wgOut->addHtml($upload[4]);
$wgOut->addHtml('</td></tr>');
}
$wgOut->addScript('<script type="text/javascript">
( function($) {
$(document).ready(function() {
$(function() {
$( "#alignment" )
.dialog({
autoOpen: false,
title: "Align Image",
//draggable: false,
resizable: false,
open: function() {
var imageName = $("#alignment").data("image-name");
// use the jQuery .html() function to put the value of 'imageName'
// into your dialog contents. (like #modal-subheader.html(imageName);)
},
buttons: {"Okay": function() {$(this).dialog("close");}},
});
$(".imageSetting")
.click(function() {
$("alignment").data("image-name", $(this).data("image-name"));
$( "#alignment" ).dialog("open");
});
});
});
} ) ( jQuery );
</script>');
$wgOut->addHTML('<div id="alignment">');
$out = self::alignment();
$wgOut->addHtml($out);
$wgOut->addHTML('</div>');

Post for from jQuery UI dialog

I am trying to integrate new functionality using jQuery UI's dialog.
I have a page with a link. When a link is clicked a modal dialog with a form in it opens. That part works OK.
$("#myForm").hide();
$("#myLink").click(function(){
$("#myForm").dialog({
modal: true,
draggable: false,
resizable: false,
width: 450,
buttons: {
"Submit": function() {
// ???
},
"Cancel": function() {
$(this).dialog("close");
}
}
});
});
Open Dialog
<div id="myForm">
<form>
<textarea id="myValues" rows="10"></textarea>
</form>
</div>
Now, I need to submit the form from within my dialog and POST results to the same page. I am getting all confused with how to implement the rest. I did look at the jQuery .post() example which made me even more confused. The page is in PHP, so when the results are submitted I need to get the post value and do some server-site action.
if (isset($_POST["myValues"])) {
// do something
}
Stuck, need help.
In jQuery function for "Submit" button:
$('form#myFormId').submit();
And in HTML:
<form id="myFormId" method="POST" action="processingscript.php">
Then the php script will get all the values that have been POSTed and can process them.
Look into using the jQuery submit function
Reference: http://api.jquery.com/submit/
Use this method that updates dialog content:
$("#myformid").dialog({
modal: true,
draggable: false,
resizable: false,
width: 450,
buttons: {
"Submit": function (event) {
event.preventDefault();
var formvalues = $("#myformid").serialize();
$.post('/Home/Edit', formvalues, function (data) {
//use data
$('#myformid').html(data);
}, "html");
},
"Cancel": function () {
$(this).dialog("close");
}
}
});
In the Control,
[HttpPost]
public PartialViewResult Edit(ViewModel model) {
//use data
return PartialView("MyDialogView", model);
}

Categories