Coderrrr
Coderrrr

Reputation: 230

Multiple dynamic contact forms

I have built a inquiry form on my website, the idea is instead of mailing me each time a user submits a query it is added to my database which I can then go and view via my backend system

Each query will be listed one by one with a text-area contact form below it allowing me to reply to each query individually

So far I have this (sorry it's a bit messy)

foreach ($listings as $row){
    $loop.= "<h3 class='text-center'>".$row['question']."</h3>";
    $loop.= "<p>".$row['message']."</p>";
    $loop.= "Name: <b>".$row['name']."</b>";
    $loop.= "<span class='pull-right'>Email: <b>".$row['email']."</b><br></span>";
    $loop.= "<div class='clearfix'></div>";
    if(isset($row['website'])){            $loop.="Website: <b>".$row['website']."</b>";        }
    $loop.= "<span class='pull-right'>Date: <b>".$row['date']."</b></span>";
    $loop.= "<form name='submit-response' method='POST'><fieldset>";
    $loop.= "<div class='form-group'>       <label for='Message".$counter."'>Your Message</label>     <textarea id='Message".$counter."' name='Message".$counter."' class='form-control' rows='5'></textarea>    </div>";
    $loop.= "<button type='submit' name='submit".$counter."' class='btn btn-default btn-block'>Reply</button>";
    $loop.= "</fieldset></form>";
}

Before that is a foreach loop and the start of the oh and $counter is set to nill

What I want is for each contact form to be unique so when I click send on one of the queries it will be sent and removed so I can send another, the only issue I am having is working out how I will work out if a submit has been hit, and which submit has been hit

The code will need to workout which button has been hit and depending on which button it will then mail() to the recipient

I'm quite stuck on this one and I'm not sure of the best course of action so any advice is really appreciated

Luke

Upvotes: 0

Views: 385

Answers (3)

MajorCaiger
MajorCaiger

Reputation: 1913

If you click a submit button inside a <form> tag, then only that form will be submitted.

You could include a hidden field with the ID of the row in it. That way you could get rid of the $counter variables altogether.

Also if you plan on just echoing out the $loop html, I wouldn't recommend storing the HTML in a PHP variable.

<?php
foreach ($listings as $row)
{
    ?>
    <h3 class="text-center"><?php echo $row['question']; ?></h3>
    <p><?php echo $row['message']; ?></p>
    Name: <b><?php echo $row['name']; ?></b>
    <span class="pull-right">Email: <b><?php echo $row['email']; ?></b><br></span>
    <div class="clearfix"></div>
    <?php
    if(isset($row['website']))
    {
        ?>
        Website: <b><?php echo $row['website']; ?></b>
        <?php
    }
    ?>
    <span class="pull-right">Date: <b><?php echo $row['date']; ?></b></span>
    <form action="" name="submit-response" method="POST">
        <input type="hidden" name="id" value="<?php echo $row['id']; ?>" />
        <fieldset>
            <div class="form-group">
                <label>Your Message</label>
                <textarea name="Message" class="form-control" rows="5"></textarea>
            </div>
            <button type="submit" class="btn btn-default btn-block">Reply</button>
        </fieldset>
    </form>
    <?php
}
?>

Upvotes: 1

user1508519
user1508519

Reputation:

Give each form a id, and possibly each submit button a unique name. This way you can easily determine which submit button was hit, or which form was submitted, and remove it or process it via javascript.

$('form').each(function() {
  $(this).submit(function(event) {
    event.preventDefault();
    // Add AJAX code here
    $(this).remove();
  });
});

Of course that was pseudo-code.

Upvotes: 0

Garytje
Garytje

Reputation: 874

add a unique id to your database table, and put it in a hidden input.

Upvotes: 0

Related Questions