Hey guys,

I'm trying to echo a link that has an onclick attribute attached to it, but when I click on the link on the page, just goes to the desired page, and doesn't preform the onclick action.

Here's my code:

<?php 
   if(session_is_registered("sesvar")){ 
      echo ("<a href='../../admin/comment-delete.php?id=$id2' onclick='return(confirm('Are you SURE you want to delete this comment??'))';><img src='../../images/delete.png' align='left' alt='Delete This Comment?'></a>"); 
   } 
?>

If anyone could show me how to make it work an alternative way, that would be great.

Thanks a lot,
Colm

Recommended Answers

All 6 Replies

the onclick doesn't work because your onclick attribute and your confirm() function are both usiing '', the they have to uses different delimiters

<?php 
   if(session_is_registered("sesvar")){ 
      echo ("<a href='../../admin/comment-delete.php?id=$id2' onclick='return confirm('Are you SURE you want to delete this comment??')';><img src='../../images/delete.png' align='left' alt='Delete This Comment?'></a>"); 
   } 
?>

try that

You can as well do it without the onclick event by simply placing the desired javascript code in the href attribute or a the function name containing your code.

/*Example*/

<script type="text/javascript">
function Confirmation()
{
if(confirm('Really delete this document?'))
window.location.href='../../admin/comment-delete.php?id=$id2';
}
</script>
<a href="javascript: Confirmation()">Delete?</a>

Hope it helps

You can as well do it without the onclick event by simply placing the desired javascript code in the href attribute or a the function name containing your code.

/*Example*/

<script type="text/javascript">
function Confirmation()
{
if(confirm('Really delete this document?'))
window.location.href='../../admin/comment-delete.php?id=$id2';
}
</script>
<a href="javascript: Confirmation()">Delete?</a>

Hope it helps

It is probably easer to keep it in the <a /> tag to keep it simple

Thanks for that advise samarudge

How about this one, How can I put an onclick event on this code:

echo "<td align='center' bgcolor=#D4E4F3><a href=delete.php?contract_num=". $row ." ><FONT COLOR=#FF0000>Delete</a></FONT></td>";

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.