使用onclick在php / html中显示确认消息

So I have this line of code:

echo '<a href="scripts/Logout.php"><span>Logout</span></a></li>';

This appears when a user is logged in, instead of displaying "Login", it says "logout". It works fine, however, what I am trying to do is use onClick to display a popup message box asking the user "are you sure you wish to logout?"

Something like this:

echo '<a href="scripts/Logout.php" onClick="return confirm('are you sure?')"><span>Logout</span></a></li>';

If for example i copy that second line of code on the home page within a content div, it works fine (minus the fact im no longer clicking on the "Logout" button).

**FYI - I have taken this out of the php script (deleted the echo infront of the line, still the same result).

Anyone able to point me in the right direction?

Many thanks.

An alternative to @JamesHunt's comment.

Assuming you're using sessions, you could verify if the user is logged in with the session itself.

Inside php file:

<?php
    if($_SESSION['userAuthed']):
?>
    <a href="scripts/Logout.php"><span>Logout</span></a></li>
<?php
    else:
?>
    <a href="scripts/Logout.php" onClick="return confirm('are you sure?')"><span>Logout</span></a></li>
<?php
    endif;
?>

The code highlighting in your code snippet already shows you what's wrong: The single quote actually breaks your echo statement. Escape them:

echo '<a href="scripts/Logout.php" onClick="return confirm(\'are you sure?\')"><span>Logout</span></a></li>';

I would generally avoid echoing a lot of html code to prevent these kind of errors in the first place.

Also UX-wise it is questionable if you want to prompt a warning. Except in the case where the user might lose some data, it is usually preferred to let them log out laconically.