简体   繁体   中英

Pull Email Address From Database Using Checkboxes (PHP, MYSQL)

I'm a beginner with PHP/MYSQL and need a little help with a project I'm working on.

I'm building a contact database for a sales person. She needs to be able to use a search term to look through her contacts for people who match that term. When she searches a term and the list of matching contacts appears, she wants to be able to select certain entries using a checkbox and then get a list of their email addresses to copy and paste into Outlook so she can send them spreadsheets of goods she has for sale or other information.

The search function (and everything else) is working perfectly, but I cannot seem to get this last part to work. Right now only the last person on the list's email address will display if that person is checked. If the last person on the list is not checked, nothing will display no matter how many other people are checked.

I have searched here already and tried to implement the suggestions I have seen but I am not getting the results I want. I'm hoping someone can help. Thank you

<? php
    if (isset($namesubmit)) {
        $sql = "SELECT * FROM contact WHERE name LIKE '%$searchName%'";
        $result = mysqli_query($connect, $sql);
        if (empty($searchName)) {
            echo '<span style="color:red">Please enter a name to search the database</span>';
        } elseif (mysqli_num_rows($result) > 0) {
            echo "<h3>Results For <b>{$searchName}</b>: <p></h3>";
            while($row = mysqli_fetch_assoc($result)) {
                $id = $row["contactID"];
                $email = $row["email"];
                $bizphone = $row['bizphone'];
                $mobphone = $row['mobphone'];
                echo '<h2>'  . $row["name"] . '<br /></h2>' . '<h3>' . $row["company"] . '<br />' . $row["website"] . '<br />' . "<a href=\"mailto:$email\">" . $email . '</a>' . '<br /> Business Phone Number: ' . "<a href=\"tel:+$bizphone\">" . $bizphone . '</a><br /> Mobile Phone Number: ' . "<a href=\"tel:+$mobphone\">" . $mobphone . '</a><br /> Business Address: ' . $row["address"] . '<br /> Buys: ' . $row["buys"] . '<br /> Sells: ' . $row["sells"]  . '<br />';
                echo  "<form method=\"POST\" action=\"updateform.php\"><input type=\"hidden\" name=\"sel_record\" value=\"$id\"><input type=\"submit\" name=\"update\" value=\"Edit Contact\"></form>" . '<p>' . "<form method=\"post\" action=\"deletecontact.php\"><input type=\"hidden\" name=\"sel_record\" value=\"$id\"><input type=\"submit\" name=\"delete\" value=\"Delete Contact\"></form><p><form method=\"POST\" action=\"emaillist.php\"><input type=\"checkbox\" name=\"action[]\" value=\"$email\" id=\"$email\">Send Email</h3><br /><hr />";
            }
            echo "<input type=\"submit\" name=\"massSubmit\" value=\"Send Email\"></form>";
?>

<? php
    $massSubmit = $_POST['massSubmit'];
    $action = $_POST['action'];
    if (isset($massSubmit)) {
        foreach ($action as $value) {
            echo $value;
        }
    }
?>

So right now it will only display the email address of the last person in the list if that person is checked. If that person is not checked it won't display anything even if others on the list are checked. Var_dump says NULL .

Thank you very much

Edit: Thank you for the information about the nested forms. I deleted the other forms as a test and it is working now the way I wanted. The problem now is how to integrate the ability to update the contact, delete the contact, and collect the email addresses all at the same time. Can anyone help with this?

If I understood your problem correctly, you want to select some options from the search results by checking some boxes and fetching additional data points. Here's a way I can see this being done:

1) Make your checkbox inputs an array. This way you can loop through them in your receiving PHP page:

<input type="checkbox" name="salesperson[]" value="<?php echo $row["contactID"];?>>"

2) In your receiving PHP page, create a loop and form an array (let's call it $contactarray ) with all selected contactId entries.

3) Create a MySql query to fetch the additional data using a WHERE clause like below:

$query = "SELECT * FROM contact WHERE contactID in (" . $contactarray . ")";

4) Get the results of that query and you'll have all the info you need.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM