using the result from window.confirm() in PHP

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
mikeglaz
Forum Newbie
Posts: 9
Joined: Sat Mar 26, 2011 5:36 pm

using the result from window.confirm() in PHP

Post by mikeglaz »

I have a website where the user can upload/manage/delete pictures. My question is how to delete the pictures. I organized it so:
http://www.mikeglaz.com/wujek/manage.php?location=art
when the user clicks on 'Delete' a window.confirm() window pops up asking if the user wants to delete the picture. I know the window returns true/false whether the user selects OK/Cancel. My question is how would I implement the PHP to delete the picture when the user selects OK? Or is this even possible?

thanks,
mike
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: using the result from window.confirm() in PHP

Post by McInfo »

Return the result to the element.

In this example, a GET request is sent if the user confirms.

Code: Select all

<a href="?item=123&action=delete" onclick="return confirm('Delete?');">delete</a>
Best practice might suggest that a POST request should be used for requests that modify data. In that case, a form is used. The form and button can be styled with CSS. You should give the form's action attribute an appropriate value. The JavaScript can be assigned to either the form's onsubmit attribute or the button's onclick attribute.

Code: Select all

<form method="post" action="" onsubmit="return confirm('Delete?');">
    <input type="hidden" name="item" value="123" />
    <input type="submit" name="action" value="delete" />
</form>
Consider what will happen if the user does not have JavaScript enabled and design the server side of your application accordingly.
mikeglaz
Forum Newbie
Posts: 9
Joined: Sat Mar 26, 2011 5:36 pm

Re: using the result from window.confirm() in PHP

Post by mikeglaz »

thank you so much, the GET method works. The only thing is that I have to have the 'confirm' within the <a> tag. What I was wanting originally was to call a function like so: onclick="return deleteImage('$name')" ... but this appends the ?item=123&action=delete to the URL no matter whether I press OK or Cancel.

and the function is:
function deleteImage(image)
{
window.confirm('Are you sure you want to delete image \'' + image + '\'?');
}
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: using the result from window.confirm() in PHP

Post by McInfo »

Tell deleteImage() to return the confirmation value.

Code: Select all

function deleteImage () {
    return window.confirm('...');
}
mikeglaz
Forum Newbie
Posts: 9
Joined: Sat Mar 26, 2011 5:36 pm

Re: using the result from window.confirm() in PHP

Post by mikeglaz »

Thanx a lot McInfo. I really appreciate it.

mike
Post Reply