Because PHP is static, and server-side, it cannot interact dynamically with the browser. Only Javascript (or VBScript), or some other client-side language can do that, because it does not have to send a new request to the server everytime that the content needs to change. There is no possible way that you can have tags inserted, dynamically, into a textarea box with PHP.
And it's javascript, not Java (two very different programming languages 😉)
Here's my updated code:
<html>
<head>
<script language="javascript">
function insertTag(tag, close) {
document.htmlForm.htmlText.value = document.htmlForm.htmlText.value + "<" + tag + ">";
if (!close) {
closingTag = "</" + tag + ">";
}
else {
closingTag = "</" + close + ">";
}
document.htmlForm.htmlText.focus();
}
function closeTag() {
document.htmlForm.htmlText.value = document.htmlForm.htmlText.value + closingTag;
closingTag = "";
document.htmlForm.htmlText.focus();
}
</script>
</head>
<body>
<a href="javascript:insertTag('b')">Bold</a> |
<a href="javascript:insertTag('i')">Italics</a> |
<a href="javascript:insertTag('u')">Italics</a> |
<a href="javascript:insertTag('a href=\'Place location here\'', 'a')">Link</a> |
<a href="javascript:insertTag('table')">Table</a> |
<a href="javascript:insertTag('tr')">Table Row</a> |
<a href="javascript:insertTag('td')">Table Cell</a> |
<a href="javascript:insertTag('br')">Break</a> |
<a href="javascript:insertTag('p')">Paragraph</a>
::: <a href="javascript:closeTag()">Close Tag</a>
<form name="htmlForm">
<textarea name="htmlText" cols=50 rows=20></textarea>
</body>
</html>
If the tag needs to be closed with something other than the original, simply add a second argument when you call the function, and this will be what will close that tag (use the Link tag example for a demonstration).