Different contact form for each item listed [closed]

0

I mean, I need a different contact form for each user when they enter a category.

That is, the website is classified ads, and the ads are listed by categories, I need a contact button that is repeated within the while loop that shows the ads, the ads are in a mysql table, which contains the email of the advertiser in each record.

The problem is to create the form within the while, with each different recipient.

Thanks in advance.

    
asked by Maseres 18.03.2017 в 06:13
source

2 answers

1

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 -->
}
    
answered by 18.03.2017 в 08:19
0

I have this:

    while ($array = mysql_fetch_array($cad)){
    <div>

    //muestro los resultados de los anuncios
    <div id="abc1">
<!-- Popup Div Starts Here -->
<div id="popupContact1">
<!-- Contact Us Form -->
        <a onclick="contacto1()"><button onClick="form6.reset();" id="submit1" style="float:right">Contactar</button></a>
        <form action="" id="form6" name="form6" autocomplete="off" method="post">
    <fieldset>
    <legend style="font-size: 16pt;"><b>Contactar con el anunciante</b></legend>
                <img id="close" src="/images/x.png" onclick ="div_hide1()" width="10" height="10">
                <?php if (isset($array["nombre"]) && $array["nombre"] != ""){ ?>
                <div>Nombre anunciante: <em style="color:#CCFFFF;"><?php echo utf8_encode($array["nombre"]); ?></em></div>
                <?php } ?>
                <?php if (isset($contactito["telefono"]) && $contactito["telefono"] != ""){ ?>
                <div><b>Teléfono: <a href="tel://<?php echo utf8_encode($array["telefono"]); ?>"   style="color:#006600;"><?php echo utf8_encode($array["telefono"]); ?></a></b></div><br>
                <?php } ?> 
                <label>Enviar mensaje al correo del anunciante</label><br>
                <label><strong>Tu email:</strong></label><br />
                <input type="email" placeholder="email" name="email" id="email" tabindex="1" required autocomplete="off" /><br /><br />
                <label><strong>Tu nombre:</strong></label><br />
                <input type="text" placeholder="Nombre" name="nombre" tabindex="2" autocomplete="off" /><br /><br />
                <label><strong>Mensaje:</strong></label><br />
                <TEXTAREA ROWS=4 COLS=32 name="mensaje" class="inputs" id="texto" tabindex="2" style="width:280px;height:142px;" maxlength="600" minlength="10" required onpaste="return false" placeholder="Mensaje..."></TEXTAREA><br>
                <input type="submit" id="submit" value="Enviar" name="entrar">

    </fieldset>
    </form>
    </div>
<!-- Popup Div Ends Here -->
</div>
<!-- Display Popup Button -->
        </div>
    <?php } // fin while ?>
    La funcion contacto1() abre el popup del formulario

        function contacto1() {
    document.getElementById('abc1').style.display = "block";
    }
    
answered by 18.03.2017 в 10:46