I've got a simple question: how can I make an onclick return confirm event work inside PHP tags.

<HTML>
<BODY>

<a href="delete.php" OnClick="return confirm('blah blah');"> Click here </a>

<?PHP

ECHO "<a href='delete.php' OnClick='return confirm('blah blah');'> Click here </a>";

?>

</BODY>
</HTML>

the first link is a simple html link with an onclick event that displays a message that allows the user to confirm the link or cancel it. canceling it will stop the link from loading.

but when I ported it inside the php tags ( and changed the "" in the link code to ' ' in order for it to work with no errore. the return confirm event will not trigger and the page will load just as though there were no onclick event.

there has to be a way to make this work with php ( since it would be absurd for it not to work inside php tags ).

So please help me out with this.

Use escape character which is a backslash (\)

<HTML>
<BODY>

<a href="delete.php" OnClick="return confirm('blah blah');"> Click here </a>

<?PHP

ECHO "<a href=\"delete.php\" OnClick=\"return confirm('blah blah');\"> Click here </a>";

?>

</BODY>
</HTML>

such a simple answere. thank you. problem solved.

you can also use single quotes to surround the text without have to escape the double quotes inside.

ex.

echo '<a href="delete.php" OnClick="return confirm('blah blah');"> Click here </a>';

just thought i would point that out.

Hi kkeith29,

This thing you provided will not work...
Because the single quotes before the blah blah will point out an error..

single quote started before <a and it ends at confirm( ... and wt after that.. there's blah blah and then again single quotes..
This will be an error..

echo '<a href="delete.php" OnClick="return confirm('blah blah');"> Click here </a>';

sorry, forgot to escape the single quotes. thanks for pointing that out.

Hi, when I use return confirm
the title of the message shows the url of my page for example: the page say http://localhost..., you can change that title?

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.