Foreach row jQuery dialog - php

I am building a div that is part of the homepage of a website. This div display a list of projects. The projects are all saved in a MySQL database. What I am trying to do is create a button next to each row that says 'Delete'. After clicking on delete, the user should be prompted with a jQuery UI dialog box asking for confirmation. When the confirms to delete the project, the project id has to be sent to a delete page(let's call it delete.php).
What I got working so far
Get list of projects from MySQL and display each of them as a row
Added a delete button at the end of each row
When the delete button is clicked, the dialog box is shown, asking for confirmation
When the user confirms deletion of project, he is sent to the delete page.
This is where I am stuck though.
On the deletion page I echo the project id to see if it works. Somehow the delete.php page always echo's the id of the first project. Meaning that jquery always sends the first project's id.
How can I solve this mystery?
$currents = new Project();
$currents = $currents->getProjects($_SESSION["user"], "finished");
echo "<table>";
foreach($currents as $current){
echo "<tr><td>" . $current["name"] . "</td>
<td><form method='post' class='deleteproject' action='/requests/request.php'/>
<input type='hidden' name='projectid' value='" . $current['id'] . "' />
<input type='button' value='Delete' class='deleteproject'></form></td></tr>";
}
echo "</table>";
Here is the jQuery;
<script>
$(function()
{
$( ".deleteproject" ).click(function() {
$( "#dialogbox" ).attr('title', 'Are you sure you want to delete this project?').dialog({
height: 100,
width: 350,
modal: true,
buttons : {
"Confirm" : function() {
$('.deleteproject').submit();
},
"Cancel" : function() {
$(this).dialog("close");
}
}
});
return false;
});
});
</script>

When doing $('.deleteproject').submit();, you always send the first .deleteproject and not the one clicked. To solve that, you can save the this reference and submit your parent form.
$( ".deleteproject" ).click(function() {
$this = $(this);
$( "#dialogbox" ).attr('title', 'Are you sure you want to delete this project?').dialog({
height: 100,
width: 350,
modal: true,
buttons : {
"Confirm" : function() {
$this.closest('form').submit();
},
"Cancel" : function() {
$(this).dialog("close");
}
}
});
side note : Your form class name is also .deleteproject, so your function will fire even if you dont click on the button, but on the form. Also, (not sure about this) i think your script will fire twice because of the event bubbling. A click on the button deleteproject is also a click on the form deleteproject.

Related

Delete function not working properly in angular?

this is my code for delete particular row in table on form in angular.
its working but not properly. Problem is when i click on delete button
only pop ups comes.after that nothing happens. when i refresh my page
on refresh that particular row deletes.
So everytime i have to refresh
page to see delete works or not? How to solve this issue so i dont
have to refresh my page evertytime after delete something.
<a ng-click="deleteInfo(detail)" onclick="return confirm('Are you sure
you wish to delete this Record?');">
You can write your confirm alert inside the ng-click function.
scope.deleteInfo = function(detail){
$ngBootbox.confirm('Are you sure you wish to delete this Record?')
.then(function() {
//either you can reload window or delete the particular ng-model or whatever
},
function() {
});
return false;
}
it's because only your onclick triggered in this scenario, you should use a custom directive to achieve this, like :
module.directive( "mwConfirmClick", [
function( ) {
return {
priority: -1,
restrict: 'A',
scope: { confirmFunction: "&mwConfirmClick" },
link: function( scope, element, attrs ){
element.bind( 'click', function( e ){
// message defaults to "Are you sure?"
var message = attrs.mwConfirmClickMessage ? attrs.mwConfirmClickMessage : "Are you sure?";
// confirm() requires jQuery
if( confirm( message ) ) {
scope.confirmFunction();
}
});
}
}
}
]);
please refere to this old post
Use sweet-alert for confirmation except "onclick" method
http://t4t5.github.io/sweetalert/

How to show popup box even if user change url to another page?

I make a popup dialog with jquery ui and ask a few question to user when they use my site. User must answer that question. If not, user can't access anything on my site.
But the problem is, when users seen popup box and not answer that question then change url to special link or some link of my site in address bar, they don't need to answer that question and can access the other page.
I don't want to access users before they are not answer question. So, I want to show my popup box even if the user change url to another page.
I made this popup question in popup.php page. Here is my code.
<?php
....//another code
....//check user session id and show dialog
...
...
echo "<div id='dialog'>";
echo "Question"."<br/>";
//select random answer for related question from database
$sql = "...get answer for question...";
$query = .....;
while($row = mysql_fetch_array($query)){
$a_id = $row['id'];
$answer = $row['name'];
echo "<input type='radio' value=\"$a_id\" name='answer' class='answer'>$answer <br/>";
}
echo "</div>";
.....
.....
<scirpt>
$("#dialog").dialog({
autoOpen:true,
width: 500,
modal: true,
resizable:false,
draggable:true,
position: {my: 'center', at: 'center', of: window, collision:'fit'},
open: function (event, ui) {
$('.ui-dialog').css('z-index',1100);
$('.ui-widget-overlay').css('z-index',1000);
$("#btn_submit").button("disable");
},
create: function(event, ui) {
$("body").css({ overflow: 'hidden' });
},
buttons: {
Submit: {
text : "Submit",
id : "btn_submit",
click:function() {
......//another code
$( this ).dialog( "close" );
$.ajax({
type:'POST',
dataType:'json',
url:'check_question.php',//this is check for user answer is correct or not
data:{..//some data....},
success: function(data){
alert("success");
},
error: function () {
alert("Error");
}
});
}
}
}
});
</scirpt>
?>
And I add this file to my home page(index.php) with include("popup.php").
Normally, I need to check user is answer or not in every page of my site.
But, its a problem for me because we have a lot of pages.
And I want to coding for checking user changing url and show popup in popup.php file.
So, Is there any way to show my popup box even if user changed the url?
First, I take current page like this $current_page="http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";.
And add current page to session and check page form session is same or not, when user request popup.php.
I hope it helps for someone.

Clicking on a dynamically created link not recognizing click event

When you click on a folder a button/link is prepended to the nav section but when i click on the added button it will not work. It does not recognize my click event for some reason..?
quick video of what i am dealing with. https://www.youtube.com/watch?v=NsW1uLbRd9w
I am clueless on why it is not working ahhhh
Any help would be awesome, thanks.
If you need more code i can send it if needed.
<script>
$(function () {
// When folder is clicked open it
$('button.<?php echo $row->folderName; ?>').bind('click', function() {
$('.open.<?php echo $row->folderName;?>').show();
// if folder is open then Prepend homebtn button to nav
var k = $('.open').css('display');
if(k == 'block') {
$('.nav').prepend('<a class="gohome">HELLO</a>');
}
});
$('.gohome').live('click', function() {
$('.open.<?php echo $row->folderName; ?>').hide();
});
});
</script>
To bind to future elements you need to bind the event to a parent that exists at the time that the procedure is executed. As your link as being added to .nav, that's what we can use. I am assuming .nav. exists when this code is run. If not, use $(document).on('click'...
$('.nav').on('click', '.gohome', function() {
$('.open.<?php echo $row->folderName; ?>').hide();
});

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

Reload Jquery UI Parent Dialog

All,
When I click a link on a web page, a JQuery UI Dialog opens up and it loads some ajax content into it. If I click a link inside this dialog, it opens up a child dialog. When I click "OK" in the child dialog, I want the child dialog to close and refresh the ajax content in the parent dialog.
Do you already have the code that closes the child dialog ? Is it an alert() call ? If so, simply add a
location.reload();
after the alert call. If it's something more complicated like a link, try
$('a.link-that-closes-dialog').click(function(){
//Code to close the dialog
location.reload();
});
i use this script for choose if show/hide any content.
After clicking button "Ritira" in the dialog, the page redirect me in the same page but whit a querystring (ex. www.mypage.ext?t=yes).
The script work, but I wish at the click of the button "Check Out" there is a refresh of the page.
I tried to enter the location.reload but did not work:
$(function() {
$(document).ready(function() {
$(".dialog-ritira").dialog({
autoOpen: false,
modal: true
});
});
$(".ritira").click(function(e) {
e.preventDefault();
var targetUrl = $(this).attr("href");
$(".dialog-ritira").dialog({
buttons : {
"Ritira" : function() {
window.location.href = targetUrl;
location.reload();
},
"Annulla" : function() {
$(this).dialog("close");
}
}
});
$(".dialog-ritira").dialog("open");
});
});
Thanks

Categories