Having a case like this:
|id_anuncio|email_usuario|
|----1-----|[email protected]|
|----2-----|[email protected]|
|----3-----|[email protected]|
In your code you should run a loop as always, for example if you use mysqli it would be something like this:
$conn = new mysqli("parametros de coneción");
$consulta = $conn -> query("SELECT * FROM table");
if( $consulta ){
while($obj = $consulta -> fetch_object()){
?>
<!-- tu codigo normal del anuncio a mostrar, imagen, descripción etc, etc -->
<form method="POST" action="/?page=product_list"><!-- aqui envias a la página que procesará la petición POST -->
<input type="hidden" name="clave" value="contacto"> <!-- un identificador para que sepas que acción debe realizarse -->
<input type="hidden" name="id_anuncio" value="<?php echo $obj -> id_anuncio; ?>"> <!-- el identificador del anuncio, este se enviará a tu script de procesamiento, allá deberás hacer otra consulta a la base de datos para obtener el correo -->
<button type="submit">Contactar al anunciante</button>
</form>
<!-- puede existir más codigo de este lado, el formulario solo mostrará un botón -->
<?php
}
}
It is not good practice to add emails directly to a form since there are scripts (BOTS) that collect emails for spam lists.
This is in two parts, here in this example you send the identifier of the advertisement to a script that will obtain by POST method the form values including the identifier. This one will have to obtain by means of a consultation to the database the mail that corresponds to said id, of there doing anything with it.
Example:
if($_POST["clave"] === "contacto" && is_numeric($_POST["id_anuncio"])){
<!-- Acá la conexión a la base de datos en una variable ($conn en este ejemplo) -->
$result = $conn -> query("select email_usuario from anuncios where id_anuncio = ".(int)$_POST["id_anuncio"]); <!-- No uses esto tal cual, es mala practica de seguridad, hay que sanear el valor -->
if( $result ){
$obj = $result -> fetch_object();
<!-- $obj ahora tiene el correo electronico correspondiente al identificador que se obtuvo de la consulta, ahora se puede hacer lo que quieras con el -->
}