[thelist] js alert to confirm deletion

Chris Blessing webguy at mail.rit.edu
Tue Sep 24 15:23:01 CDT 2002


dunstan-

Use the confirm() function in a user-defined function as such (watch the
line wrap):

<?php
$current_url = $_SERVER['REQUEST_URI'];
?>

<?php
// assume code goes here to get the imageid
?>

<a href="javascript:confirmDelete(<?php =$imageid ?>);" title="Delete this
image">delete</a>

<script language="JavaScript">
function confirmDelete(imageId){
	var currentURL = '<?php =$current_url ?>';
	var okToDelete = confirm('Are you sure you want to delete this image?');

	if(okToDelete){
		location.href = currentURL + '&DelPic=' + imageId;
	}
}
</script>


What happens here is that you get the current page variable all setup via
PHP, then pass the image id into the javascript function.  You use PHP to
write the current url variable to a javascript variable (to be interpreted
on the client side in the web browser) and if the user confirms the delete
warning (i.e. clicks 'Ok' or 'yes') then the browser will load up the
currentURL + the DelPic attribute.

You could also use JavaScript to get the current URL, but it's more reliable
and easier to do it server-side with PHP like you've done.

HTH!

Chris Blessing
webguy at mail.rit.edu
http://www.330i.net

> I'd like to put a JS alert in there so that the user has to click on it to
> confirm they want the delete to go ahead. And if the click 'NO'
> then the page
> stays as it was.
>
>
> Could anyone suggest how to do that? I can't work it out :o/
>
>
> Thanks very much - dunstan




More information about the thelist mailing list