Page 1 of 1
using the result from window.confirm() in PHP
Posted: Fri Apr 01, 2011 4:56 pm
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
Re: using the result from window.confirm() in PHP
Posted: Fri Apr 01, 2011 6:55 pm
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.
Re: using the result from window.confirm() in PHP
Posted: Fri Apr 01, 2011 7:45 pm
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 + '\'?');
}
Re: using the result from window.confirm() in PHP
Posted: Fri Apr 01, 2011 9:48 pm
by McInfo
Tell deleteImage() to return the confirmation value.
Code: Select all
function deleteImage () {
return window.confirm('...');
}
Re: using the result from window.confirm() in PHP
Posted: Sat Apr 02, 2011 12:18 pm
by mikeglaz
Thanx a lot McInfo. I really appreciate it.
mike