Ciao, puoi usare una funzione del genere
codice:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Test</title>
<script type="text/javascript" language="javascript">
/* Parametri: id del textarea, tag da inserire */
function add_text(obj_id,tag){
document.getElementById(obj_id).value = document.getElementById(obj_id).value + '<'+tag+'>' + prompt('Inserisci testo') + '</'+tag+'>';
}
</script>
</head>
<body>
<input type="button" onclick="add_text('txtarea','b')" value="BOLD" />
<textarea name="txtarea" id="txtarea"></textarea>
</form>
</body>
</html>
Questo per come lo hai richiesto tu, però io, più che mettere il tag html, userei una cosa tipo BBCode ad esempio questo (la pagina si chiama test.php)
codice:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Test</title>
<script type="text/javascript" language="javascript">
/* Parametri: id del textarea, tag da inserire */
function add_text(obj_id,tag){
document.getElementById(obj_id).value = document.getElementById(obj_id).value + ' ['+tag+']' + prompt('Inserisci testo') + '[/'+tag+'] ';
}
</script>
</head>
<body>
<form action="test.php" method="post">
<input type="button" onclick="add_text('txtarea','b')" value="B" style="font-weight:bold" />
<textarea name="txtarea" id="txtarea"></textarea>
<input type="hidden" value="1" name="sendform" />
<input type="submit" value="invia" />
</form>
<?php
/* Se il form è stato inviato */
if(isset($_POST['sendform'])){
/* Leggo il contenuto del textarea */
$text = $_POST['txtarea'];
/* Elimina tag html */
$text = strip_tags($text);
/* Aggiungo gli escape */
$text = addslashes($text);
/* Aggiunge i br */
$text = nl2br($text);
/* Sostituisco i [b] con il tag html, puoi sostituire 'b' con qualsiasi altro tag */
$text = preg_replace('#\[b\](.*?)\[/b\]#si', '\1', $text);
/* $text può essere messo nel db */
/* Qui stampo di nuovo text */
echo stripslashes($text);
}
?>
</body>
</html>