Jump to content

Echoing a javascript Alert from .php


Mavent

Recommended Posts

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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;

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.