Daniel_V
Daniel_V

Reputation: 183

Trigger email to the corresponding email address

I'm generating a table using this code:

while ($row = mysql_fetch_array($result)) {
        echo "<tr>";
            echo "<td>{$row['rollNo']}</td>";
            echo "<td>{$row['bgroup']}</td>";
            echo "<td>{$row['first_name']}</td>";
            echo "<td>{$row['last_name']}</td>";
            echo "<td>{$row['phone']}</td>";
            echo "<td>{$row['e_mail']}</td>";
            echo "<td><input type='button' value='Send Email'></td>";
        echo "</tr>";

The output:

Generated table

When I click on Send Email, I want to trigger an email to the email corresponding to the button. How can I send the email address beside the button as input to the mail() function?

Thanks in advance!

Upvotes: 0

Views: 43

Answers (1)

Rasclatt
Rasclatt

Reputation: 12505

Further to your comment for clarity, I would do something like this:

// Top of page:
if(isset($_POST['submit'])) {
    // check the $_POST['id'] against your db
    // if you have it in the db (you should!)
    // send using the mail()
}


while($row = mysql_fetch_array($result)) { ?>
    <tr>
        <form method="post" action="">
            <td><?php echo $row['rollNo']; ?></td>
            <td><?php echo $row['bgroup']; ?></td>
            <td><?php echo {$row['first_name']; ?></td>
            <td><?php echo $row['last_name']; ?></td>
            <td><?php echo $row['phone']; ?></td>
            <td><?php echo $row['e_mail']; ?></td>
            <td>
                <!-- hidden input here with the id (or whatever your auto incremented/unique value is in your table) -->
                <input type="hidden" name="id" value="<?php echo $row['id']; ?>">
                <input type="submit" name="submit" value="Send Email"></td>
        </form>
    </tr><?php
    } ?>

As a side note, you should not be using the mysql_ library/functions. It is insecure and scheduled for deprecation in future versions of PHP. One day you will wake up and find none of your db code works anymore!

Upvotes: 1

Related Questions