In addition to what tandu said:
echo "<br><font face='Segoe UI' color='blue'><a href='#' onClick='javascript:alert(".$nam.");return false;'>".$nam."</a></font>";
You're really making your life hard here by mixing together PHP, HTML, styling, scripting, and a string literal inside JavaScript.
Each time you put one kind of string inside another kind of string you need a type of escaping, otherwise the string can break out of its context. For example consider what happens if $nam contains a single-quote or double-quote. In a situation where the data you are dealing with is user-submitted, that's not just a bug, it's an XSS security hole.
Every time you insert a text string into HTML, you need to use htmlspecialchars(). And every time you insert a text string into a JavaScript literal, you need to backslash-escape it. addslashes() is almost but not quite good enough for that. json_encode() is much better as it can convert any suitable PHP value into a JavaScript literal, not just strings, and does is as a complete literal including the quotes so you don't have to add them. So:
echo "<br><font face='Segoe UI' color='blue'><a href='#' onClick='alert(".htmlspecialchars(json_encode($nam)).");return false;'>".htmlspecialchars($nam)."</a></font>";
This is of course an unreadable mess. Let's separate each layer to simplify, and do it using PHP itself rather than trying to hack around with HTML strings. PHP is a templating language, you might as well use it!
<style type="text/css">
#alerter {
display: block;
color: blue;
font-family: "Segoe UI", sans-serif;
}
</style>
...
<a id="alerter" href="#">
<?php echo htmlspecialchars($nam); ?>
</a>
...
<script type="text/javascript">
document.getElementById('alerter').onclick= function() {
alert(<?php echo json_encode($nam); ?>);
return false;
};
</script>
This is much easier to cope with and easily extensible to style and script lots of similar links at once. You can grab the value of $nam from the DOM, too, to avoid having to state it twice, once in HTML and once in JS. You can also define functions with short names to avoid having to type htmlspecialchars() and json_encode() so much.
<?php
function h($s) {
echo htmlspecialchars($s, ENT_QUOTES, 'utf8');
}
?>
<style type="text/css">
.alerter {
display: block;
color: blue;
font-family: "Segoe UI", sans-serif;
}
</style>
...
<a class="alerter" href="#"><?php h($nam); ?></a>
<a class="alerter" href="#"><?php h($nam2); ?></a>
...
<script type="text/javascript">
for (var i= document.links.length; i-->0;)
if (document.links[0].className==='alerter')
document.links[0].onclick= function() {
alert(this.firstChild.data);
return false;
};
</script>
This is an example of ‘unobtrusive scripting’.