The easiest way is probably a link like
<a href="reply.php?topicid=1234">Reply to this topic</a>
In reply.php have a hidden field in the message reply form
$topicid=$_GET['topicid'];
<input type="hidden" value="<?=$topicid?>" name="topicid>
The topicid will then be returned with the reply.
hth