Web Analytics Made Easy -
StatCounter Email Form results to variable recipients - CodingForum

Announcement

Collapse
No announcement yet.

Email Form results to variable recipients

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Email Form results to variable recipients

    I have a form designed that needs to have the results emailed to various people. Each time the form is completed the person completing the form can choose who to send the results to from a list of names obtained from a dropdown list in the form (the names and email addresses are stored in the database). Is it possible to select the individuals who will receive the email, and then somehow email it to them. The results of the form will also be saved in a database.

    I am able to get the names into the form and have it developed to allow you to select multiple names, and also have the basic email developed, but am not sure how to get these names into the email recipeients list so it will be sent to them.

    Any help is greatly appreciated.

    Ken

  • #2
    create a dropdown list in html, name the list to, give the drop down the value of the email address to send to,
    Code:
    <form method="post" action="sendmail.php">
    <select name="to">
    <option value="[email protected]">jesus</option>
    <option value="[email protected]">Beep</option>
    <option value="[email protected]">Flat</option>
    <option value="[email protected]">saudi</option>
    </select>
    </form>
    now put this on your sendmail.php
    PHP Code:

    <?php 
    $message 
    "$_POST['message']";
    $to "$_POST['to']";
    $subject "$_POST['subject'];

    mail(
    $to$subject$message);

    if(mail){ 
    echo "
    <BR />Thanksmessage sent"; 

    else{ 
    echo "
    <BR /> An Error occured"; 
    }  //or change the echo to if (mail(
    $to$subject$body)) { header 
    //Location: sent.html"
    ); }
    //else { header("Location: error.html"); }
    ?>

    Comment


    • #3
      Notice how the name of my select field is followed by []. This will create an array of options when posted - otherwise, you'll end up with a single option.

      PHP Code:
      <?php
          
      if($_POST)
          {
              
      $to '';

              foreach(
      $_POST['recipients'] as $recipient)
                  
      $to .= $recipient.',';

              
      // Ready $to for mail() 
              
      $to substr($to,0,-1);

              echo 
      $to;
          }
      ?>
      Code:
      <form method="post" action="#">
      	<select name="recipients[]" multiple>
      		<option value="[email protected]">Joe</option>
      		<option value="[email protected]">Bill</option>
      		<option value="[email protected]">Wendy</option>
      	</select> 
      	<br />
      	<input type="submit" />
      </form>
      Last edited by syosoft; Oct 2, 2006, 05:15 AM.
      Active PHP/MySQL application developer available for immediate work.
      syosoft.com mavieo.com - Remote Web Site Administration Suite - Reseller Ready

      Comment


      • #4
        Thanks for the suggestions on how to do this. I notice in both of the examples it requires that I have the action going to a different page. Due to other fields in the form, I need to have the action statement work on the same page as the form. Is this possible?

        Ken

        Comment

        Working...
        X