SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Member
    Join Date
    May 2010
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Form to Email code problems (while - looping)

    I have a form on a page which is accessed when a user logs into their account.
    The form dynamically pulls bits of the users info from 2 tables in a mysql database, Tables (ITEMS) and Table (USERS).
    Table (USERS) holds the users info (NAME, EMAIL ETC) while Table (ITEMS) hold information about the users items for which they may have several of.
    When the form page is accessed all the users items from Table (ITEMS) are dynamically shown on the form with several radio buttons at the side of each item.
    The user can tick certain radio buttonss at the side of each item and click the send button.

    TRYING TO ACHIEVE...
    After the form is sent a thank you message is returned on the same page with details of the items and what radios were ticked.
    An Email of the same is sent to user with info of the items and what radios were ticked.
    An Email is also sent to me with same info of the items and what radios were ticked.

    WHAT I AM ACHIEVING SO FAR WITH CODE BELOW:

    The form page is ok showing all users items, radios, send button etc.
    The returned thank you page is fine showing items, what radios were checked etc.
    Even the email part is working sending an email to the user and myself.

    THE PROBLEM IS THAT A SEPERATE EMAIL IS SENT TO BOTH OF US FOR EACH ITEM THE USER HAS IN THE DATABASE.
    I JUST WANT ONE EMAIL SENDING TO BOTH OF US DETAILING ALL ITEMS.

    I realise this is because of the while statement in the call to the database which is looping the below code for each item thus sending an email for each item.

    What I can not suss out is how to send just the one email with all items on.
    Tried moving the closing loop bracket all over the place and parts of the code but to not avail.

    THE CODE: (some unecessary code removed from echo/thank you to simplify)
    PHP Code:
    <table width="100%">
    <tr>
    <td class="bodytext"><strong>Name</strong></td>
    <td class="bodytext"><strong>Ref</strong></td>
    <td><strong>Status</strong></td>
    <td><strong>Type</strong></td>
    <td><strong>Date</strong></td>
    <td><strong>Extend</strong></td>
    <td><strong>Feature</strong></td>
    <td><strong>Offer</strong></td>
    </tr>

    <form action="example-this-page" method="post">
    <input type="hidden" name="action" value="0">
    <input type="hidden" name="id" value="<?php echo $_REQUEST["id"]; ?>">

    <?php 
    $sql 
    "SELECT * FROM ".$TABLES["items"]." WHERE user_id='".$_SESSION["UserAccount"]."'";
    $sql_result mysql_query ($sql$connection ) or die ('request "Could not execute SQL query" '.$sql);

    while (
    $ITEMS mysql_fetch_assoc($sql_result)) {

    $sql "SELECT * FROM ".$TABLES["users"]." WHERE id='".$ITEMS["user_id"]."'";
    $sql_resultT mysql_query ($sql$connection ) or die ('request "Could not execute SQL query" '.$sql);

    $USER=mysql_fetch_assoc($sql_resultT);

    ?>

    <? if (isset($action)) {

    $to $USER["email"];
    $mailheader "From: $email\r\n";
    $mailheader .= "Reply-To: $email\r\n";
    $mailheader .= "Content-type: text/html; charset=iso-8859-1\r\n";
    $msg =

    "Dear ".stripslashes($USER["name"])."<br><br> 
    <table><tr>
    <td><strong>Name</strong></td>
    <td><strong>Ref No</strong></td>
    <td><strong>Status</strong></td>
    <td><strong>Type</strong></td>
    <td><strong>Date</strong></td>
    <td><strong>Extend</strong></td>
    <td><strong>Feature</strong></td>
    <td><strong>Offer</strong></td>
    </tr>
    <tr>
    <td>"
    .stripslashes($ITEMS["name"])."</td>
    <td>"
    .stripslashes($ITEMS["ref"])."</td>
    <td>"
    .stripslashes($ITEMS["status"])."</td>
    <td>"
    .stripslashes($ITEMS["type"])."</td>
    <td>"
    .stripslashes($ITEMS["date"])."</td>
    <td>"
    .$_POST['extend']."</td>
    <td>"
    .$_POST['feature']."</td>
    <td>"
    .$_POST['offer']."</td>
    </tr>
    </table>
    "
    ;

    mail($to"subject"$msg$mailheader) or die ("Failure");
    mail("me@me.co.uk""subject"$msg$mailheader) or die ("Failure");

    echo 
    "<br /><br /><font size=2>
    <strong><center>Thank you!</center><br />
    Your details of your request has been sent:</center><br /> 
    "
    ;

    };

    ?>

    <tr>
    <td><?php echo stripslashes($ITEMS["name"]); ?></td>
    <td><?php echo stripslashes($ITEMS["ref"]); ?></td>
    <td><?php echo stripslashes($ITEMS["status"]); ?></td>
    <td><?php echo stripslashes($ITEMS["type"]); ?></td>
    <td><?php echo stripslashes($ITEMS["date"]); ?></td>

    <td><input type="radio" name="extend<?php echo stripslashes($ITEMS["id"]); ?>" value="No"> No 
    <input type="radio" name="extend<?php echo stripslashes($ITEMS["id"]); ?>" value="Yes" /> Yes</td>
    <td><input type="radio" name="featured<?php echo stripslashes($ITEMS["id"]); ?>" value="No"> No 
    <input type="radio" name="featured<?php echo stripslashes($ITEMS["id"]); ?>" value="Yes" /> Yes</td>
    <td><input type="radio" name="latedeal<?php echo stripslashes($ITEMS["id"]); ?>" value="No"> No 
    <input type="radio" name="latedeal<?php echo stripslashes($ITEMS["id"]); ?>" value="Yes" /> Yes</td>
    </tr>
     
    <?php ?>

    <tr> 
    <td>&nbsp;<input type="submit" name="Submit" value="Send"></td>
    </tr></table>
     
     
    </form>
    </table>
    Last edited by ScallioXTX; Sep 20, 2010 at 06:41. Reason: Wrapped code in [php] tags

  2. #2
    Non-Member Kalon's Avatar
    Join Date
    Aug 2010
    Location
    At my computer
    Posts
    2,012
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The PHP manual on mail() shows you how to use additional headers like cc: or bc: to send 1 email to multiple recipients.

  3. #3
    SitePoint Member
    Join Date
    May 2010
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi thanks for your reply, but I think you are missing the point!
    I don't need to send to multiple users just one user (the user accessing the form at the time) and me - which I have working ok.
    The code is looping through the users items and sending an email to both the user and me for each item the user has.
    I want one email to both the user and me with all items on.
    Thanks again!

  4. #4
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,139
    Mentioned
    16 Post(s)
    Tagged
    3 Thread(s)
    That is happening because you are calling the mail function within the while loop used to fetch each row of the result set. In order to achieve your desired functionality you would collect all items before sending the email. Thus, the mail function should follow the while loop not be inside it. Of course, it isn't as simple as that, but that is the methodology to follow, making the proper programmatic changes as necessary to achieve it.

  5. #5
    Non-Member Kalon's Avatar
    Join Date
    Aug 2010
    Location
    At my computer
    Posts
    2,012
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by zorro20 View Post
    Hi thanks for your reply, but I think you are missing the point!
    I don't need to send to multiple users just one user (the user accessing the form at the time) and me - which I have working ok.
    The code is looping through the users items and sending an email to both the user and me for each item the user has.
    I want one email to both the user and me with all items on.
    Thanks again!
    ok, I have missed something then.

    I assume "the user accessing the form" and the "me" in red are 2 different people.

  6. #6
    SitePoint Member
    Join Date
    May 2010
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Kalon View Post
    ok, I have missed something then.

    I assume "the user accessing the form" and the "me" in red are 2 different people.
    mail($to, "subject", $msg, $mailheader) or die ("Failure"); USER
    mail("me@me.co.uk", "subject", $msg, $mailheader) or die ("Failure"); ME

    Both working.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •