I am writing a simple forum and on home page there is a list of post titles. I want to let the user click on the post title and link to postPage.html. On postPage.html I need to get the title and content from firebase database. The problem is I don't know which post the user clicked on.
I've been thinking of using document.onclick to get what title the user clicked so I can use the title as a path to find the post content in firebase
firebase.database().ref(postTitle).set({post_title: postTitle...})
but it could be a problem if there are two posts with the same title.
Below is how I show the list of post titles on home page.
var str_1 = "<tr><td><a href='postPage.html'>";
var str_2 = "</td></tr>\n";
postRef.once('value').then(function(snapshot){
snapshot.forEach(function(childSnapshot){
var childData = childSnapshot.val();
total_post[total_post.length] = str_1 + childData.post_title + str_2;
});
document.getElementById('allposts').innerHTML = total_post.join('');
}).catch(e => console.log(e.message));<br/>
and the html looks like this
<table class="table table-hover">
<thead>
<tr>
<th>Topics</th>
<th>Posted by</th>
</tr>
</thead>
<tbody id="allposts">
<tr>
<td><a href="postPage.html">title1</a></td>
<td>person1</td>
</tr>
<tr>
<td><a href="postPage.html">title2</a></td>
<td>person2</td>
</tr>
</tbody>
</table>
I can't think of another way to do this. Any suggestion and hint will be of great help.