user1751581
user1751581

Reputation: 87

How Would I Return Two Multiple Select Values In HTML?

I have a mysql database table called "leads" with an id column, a name column, and an email column. I currently have users selecting a lead from a multiple select box, which is returned as an array from an html form with $_POST method. I have the value for the select box items as the email column, and what is shown to the user on the front end is the name column. However, I want to be able to return two values, the email and the name, for later use. I need to have the email value(s) able to be imploded with commas between them, so I can later send them emails using PHP's mail function. However, when a user looks at sent emails, I want to show them the name value(s). I have read that I can either parse the array, or use the id column set as the value, which seems more practical, however, I am not sure how I would go about this. Any ideas? Thanks in advance for any help!

*UPDATE*

Basically, the users that log into the site have their own leads in a mysql database. I need them to be able to go to a 'send email' page, where they can select a lead, or multiple leads, by name. Then there is a subject field, and a message field so they can write the email, and then I have a 'send email' form submit button. After they click 'send email' I want to get back the email addresses and names of the leads they selected, for later use. I need to have the email addresses separated by commas, so that I can send them through a php mail function... But the important thing is, I want the info from the form to be stored in a database table called 'PendingEmails', and I am going to run a cron job to run a script that finds what emails need to be sent, and sends them... After an email is sent, I need to put the names, subject, and message from the email in a separate database, called 'SentEmails'. On a different page on the site, I want the users to be able to see all the emails they sent, but I want them to see the names of who they sent it to, rather than the email addresses. This is why I need both the names and the emails. I need emails for the mail function, and names to be displayed back to the user. I like your idea of using the id and running the rest server side, I just dont know how I would do this. Hope this helped.

Here is the select box:

<select data-placeholder="Select Lead(s) To Email..." multiple="true" class="chzn-container-multi" name="selectleads[]"style="width:505px;">
            <?php
do {  
?>
            <option value="<?php echo $row_rsAllLeads['Email']?>"><?php echo $row_rsAllLeads['FullName']?></option>
            <?php
} while ($row_rsAllLeads = mysql_fetch_assoc($rsAllLeads));
  $rows = mysql_num_rows($rsAllLeads);
  if($rows > 0) {
      mysql_data_seek($rsAllLeads, 0);
      $row_rsAllLeads = mysql_fetch_assoc($rsAllLeads);
  }
?>
          </select>

And here is the actual form action:

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form")) {
    $startcode = $_POST['messagefield'];
    $replaced = preg_replace( '/\\\\(?="|\')/', '', $startcode );
    $selected = $_POST['selectleads'];
    $collectedleads = implode(", ", $_POST['selectleads']);
 $to = $collectedleads;
 $subject = $_POST['subjectfield'];
 $body = $replaced;
 $headers = "MIME-Version: 1.0\r\n"; 
 $headers .= "Content-type: text/html; charset=iso-8859-1\r\n"; 
 $headers .= "From: " . $row_rs_CurrentUser['FirstName'] . " " . $row_rs_CurrentUser['LastName'] . " <" . $row_rs_CurrentUser['Email'] . ">";
 if (mail($to, $subject, $body, $headers)) {
  } else {
   echo("<p>Message delivery failed...</p>");
  }

}

The code you gave me and I tried (and failed) to edit and make work:

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form")) {
    $startcode = $_POST['messagefield'];
    $replaced = preg_replace( '/\\\\(?="|\')/', '', $startcode );

    mysql_select_db($database_myBackOfficeConn, $myBackOfficeConn);

    $collectedleads = implode(", ", $_POST['selectleads']);

//arrays to store email addresses and names
$emails = array();
$names = array();

foreach ($collectedleads as $id) {
  mysql_query("SELECT Email, FullName FROM Leads 
    WHERE Id = " . mysql_real_escape_string($id));

  while ($row = mysql_fetch_assoc()) {
    $emails[] = $row['Email'];
    $names[] = $row['FullName'];
  }
}
 $to = $collectedleads;
 $subject = $_POST['subjectfield'];
 $body = $replaced;
 $headers = "MIME-Version: 1.0\r\n"; 
 $headers .= "Content-type: text/html; charset=iso-8859-1\r\n"; 
 $headers .= "From: " . $row_rs_CurrentUser['FirstName'] . " " . $row_rs_CurrentUser['LastName'] . " <" . $row_rs_CurrentUser['Email'] . ">";
 if (mail($to, $subject, $body, $headers)) {
  } else {
   echo("<p>Message delivery failed...</p>");
  }

}

?>

Upvotes: 0

Views: 318

Answers (2)

David John Welsh
David John Welsh

Reputation: 1569

I think it would make more sense to do the retrieving of data on the server-side. Instead of the email addresses, set the <option> values to your database's id column values.

EDIT: Changed most of the answer after discussing with OP

Bearing in mind that the code you posted was just a snippet, and so I have no way of testing this, you could try something along these lines:

<select data-placeholder="Select Lead(s) To Email..." multiple="true" 
  class="chzn-container-multi" name="selectleads[]"style="width:505px;">
<?php   
  /* I think while is better, in case there are no results. 
    do...while would attempt to echo the first result in a set even 
    if there were no results, which would give an error, methinks. */

  //Use the id field of your database when you populate the select
  while ($row_rsAllLeads = mysql_fetch_assoc($rsAllLeads)) {  ?>
  <option value="<?php echo $row_rsAllLeads['id']?>">
    <?php echo $row_rsAllLeads['FullName']?></option>
<?php
  }

  /* What is this block used for? */
  $rows = mysql_num_rows($rsAllLeads);
  if($rows > 0) {
    mysql_data_seek($rsAllLeads, 0);
    $row_rsAllLeads = mysql_fetch_assoc($rsAllLeads);
  }
?>
</select>

I'm uncertain as to the purpose of the code block at the end so I just left it. I assume you do something else with that data...?

So now your <select> is exactly the same as before, but instead of actual email addresses it will hand back the corresponding database ids instead.

Now, on submitting the data, we change our tack slightly:

<?php
$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form")) {
  $startcode = $_POST['messagefield'];
  $replaced = preg_replace( '/\\\\(?="|\')/', '', $startcode );
  $selected = $_POST['selectleads'];

  /* NEW CODE */
  /*selectleads[] is now an array of your database's ids. 
    You can now run a query to get whatever information you 
    need. In this case, you want the email addresses, so:
   */
  $collectedleads = $_POST['selectleads'];
  $emailaddylist = array();
  foreach ($collectedleads as $id) {
    $x = mysql_query("SELECT `Email` FROM `leads` WHERE `id` = " . 
       mysql_real_escape_string($id));
    $x_assoc = $x->fetch_assoc();
    $emailaddylist[] = $x['Email'];
  }
  /*Now you have an array of email addresses that correspond to 
    the ids you were sent via $_POST.
   */
  $emailaddystring = implode(", ", $emailaddylist);

  $to = $emailaddystring;

  /*If you want to use any other data from the database, 
     you still have an array of ids in $collectedleads.
     Just run another query to get the data you want.*/

  /* END OF NEW CODE */

  $subject = $_POST['subjectfield'];
  $body = $replaced;
  $headers = "MIME-Version: 1.0\r\n"; 
  $headers .= "Content-type: text/html; charset=iso-8859-1\r\n"; 
  $headers .= "From: " . $row_rs_CurrentUser['FirstName'] . " " . $row_rs_CurrentUser['LastName'] . " <" . $row_rs_CurrentUser['Email'] . ">";
  if (mail($to, $subject, $body, $headers)) {
    } else {
      echo("<p>Message delivery failed...</p>");
    }
}
?>

Because we are handed the ids in an array, we can use them to get whatever information we want from the database. In this case we're just getting the email addresses. If you want to retrieve any other information, you can use the same technique - the list of ids still exists in $collectedleads, so you can run another query to get whatever data you need.

After an email is sent, I need to put the names, subject, and message from the email in a separate database, called 'SentEmails'

In that case, use the id data in $collectedleads to run a SELECT query on leads to get the information you need (name, email address, whatever), and run an INSERT query on the SentEmails table to copy that information.

Hope this helps.

CAVEAT:

The use of mysql is discouraged in favour of mysqli. To quote the page:

If you are using MySQL versions 4.1.3 or later it is strongly recommended that you use the mysqli extension instead.

It works almost exactly the same; in most cases all you need to do is to put the i in front of it. Some functions don't work with it, or have better alternatives. You should check it out.

Upvotes: 1

chechopeefe
chechopeefe

Reputation: 178

What I would do, I would add a hidden field named something along the lines of "lead_name" and fill it with jQuery or just plain JS on an onblur event with the value of the selected lead.

$("#lead_name").val($("#lead option:selected").text());

Upvotes: 0

Related Questions