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?