Mavent Posted December 24, 2011 Share Posted December 24, 2011 Hello all; I need to throw up a confirmation alert before allowing a database deletion. Here's the code I wrote: echo "<a href='task.php?option=delete\"onclick=\"return (confirm('Do you really want to delete this?'));>Delete</a>"; The idea is that the Javascript prevents the .php from completing unless the user Confirms. It works great as .html, but I've clearly done something wrong in the .php version, as it never invokes the alert. Does anyone have any idea where I went so horribly, horribly wrong? Thanks! Kyle Quote Link to comment https://forums.phpfreaks.com/topic/253806-echoing-a-javascript-alert-from-php/ Share on other sites More sharing options...
scootstah Posted December 24, 2011 Share Posted December 24, 2011 echo "<a href=\"task.php?option=delete\" onclick=\"return confirm('Do you really want to delete this?');\">Delete</a>"; However, this is absolutely not a secure way to do this. You need to use a form, so that you can validate the request to make sure the user actually meant for this to happen. Quote Link to comment https://forums.phpfreaks.com/topic/253806-echoing-a-javascript-alert-from-php/#findComment-1301181 Share on other sites More sharing options...
Pikachu2000 Posted December 24, 2011 Share Posted December 24, 2011 You have some quoting and spacing issues. echo "<a href=\"task.php?option=delete\" onclick=\"return (confirm('Do you really want to delete this?'))\";>Delete</a>"; Or you may find it easier to use HEREDOC syntax when you need to use a bunch of mixed quotes, so you don't have worry about escaping them. echo <<<HERE <a href="task.php?option=delete" onclick="return (confirm('Do you really want to delete this?'))";>Delete</a> HERE; // OR $text = <<<HERE <a href="task.php?option=delete" onclick="return (confirm('Do you really want to delete this?'))";>Delete</a> HERE; echo $text; Quote Link to comment https://forums.phpfreaks.com/topic/253806-echoing-a-javascript-alert-from-php/#findComment-1301182 Share on other sites More sharing options...
Mavent Posted December 24, 2011 Author Share Posted December 24, 2011 Thank you very much to both of you! Scootsah, you're completely correct that I need to do this through a form. This particular code is just for testing purposes, and I'm the only one that will ever use it. When the site goes Live, I'll definitely use a form as you suggest. Picachu2000, I've heard about HEREDOC before, but I'd never seen an actual code sample in action. After seeing your examples, I wish I'd been using it, as it could have saved me a TON of time. Your examples will be the basis of a lot of my future code. Again, thanks to both of you! Kyle Quote Link to comment https://forums.phpfreaks.com/topic/253806-echoing-a-javascript-alert-from-php/#findComment-1301183 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.