Anthony
Anthony

Reputation: 481

Link not asking user to confirm

I have this code

echo '<div class="bottomright"><a href="?id='
                . $topic_id
                . '&part=5"><img src="../assets/icons/Comments-    edit.png" /></a><a href="?id='
                . $topic_id
                . '&part=6"><img src="../assets/icons/Lock.png" /></a><a href="#" onclick="return confirm("Are you sure?")"><img src="../assets/icons/Trash.png" /></a></div>'; 

When I click Trash.png it is supposed to ask "Are you sure"? But it doesn't...

Am I missing something here?

Upvotes: 1

Views: 60

Answers (2)

XCS
XCS

Reputation: 28137

<a href="#" onclick="return confirm(\'Are you sure?\')">

You have to escape single quotes.

Because:

  • onclick="return confirm("Are you sure?")" is not JavaScript valid. (notice " conflict)

  • onclick="return confirm('Are you sure?')" would not be PHP valid (notice ' conflict because the whole echo function is wrapped inside single quotes)

Another option, would be to do this:

'<a href="#" onclick="return confirm('. "'Are you sure?'" .')">'

Which actually closes the echo quotes, concatenates the 'Are you sure?' and concatantes the rest.

Upvotes: 0

Steven Liao
Steven Liao

Reputation: 3787

Here's the problem:

onclick="return confirm("Are you sure?")"

Note the quote ambiguity. You have to change the quote symbols somehow to pair properly, probably by escaping a set of single quotes. Both of the following work:

onclick="return confirm(\'Are you sure?\')"

onclick=\'return confirm("Are you sure?")\'

Upvotes: 1

Related Questions