My problem is not a problem, it is a doubt.
I have a text area in which the user writes a comment that will be published on my website, but I also have a command code that by pressing, for example /
in the text area, a list with friends is displayed of the user to tag it in said comment similar to the @
when you are going to tag someone in fb, instagram etc ..
So far, what I do not know how to do is when that list of users unfolds (as you know you have to click on the user you want), tag in that comment so that the name of that user too is added to the text area. Then in the submit, save the information in the database with the comment and the person tagged in that comment.
How would that be done? I will leave my code of what I am doing. I would appreciate any help or tips about my doubt. Thanks !.
tag.php
<div>
<form id="tagcoment" action="tagcoments.php" method="post">
<h2 class="texto">write your coment</h2>
<div class="input-group col-12" >
<div id="myUsers" style="display:none;"></div>
<textarea><textarea>
</div>
</form>
</div>
tag.js
$(document).ready(function() {
$('#tagcoment textarea').on('keypress', function() {
var target=$(this).val()//obtenemmos lo escrito en el textarea
if(target.substr(0,1)=="/"){//si del 0 al 1 es igual al comando / buscar amigos
var target= target.substr(2);//enviamos es texto despues del comando /
$.ajax({
type: "POST",
url:tagcoments.php,
data:{target:target},
success: function(response) {
$("#myUsers").slideDown("fast").html(response);
}
});
}
});
});
tagcoments.php
<?php
session_start();
include"conexion.php";
$target=mysqli_real_escape_string($conexion,$_POST["target"]);//obtenemos lo escrito
$my=$_SESSION["id"];
$sql=mysqli_query($conexion,"SELECT foto,nombre,apellido FROM
registro r
INNER JOIN amigos a ON r.id=a.para
WHERE a.de='$my' AND r.nombre like '%$target%' ");//esta consulta me devuelve mis amigos segun el texto que vaya introduciendo
while($row=mysqli_fetch_array($sql)){ ?>
<!--imprimimos amigos segun el texto-->
<div>
<a href=""> <img src="imagenes/<?php echo$row["foto"]?>"><?php $row["nombre"]." ".$row["apellido"]?> </a>
</div>
As you can see, the code is very simple, everything works but besides that I want that at the time of the list of friends to unfold that when I click on one of those friends I add the name of that friend to the textarea as I told you, it's something similar to the @
commands.