1

I have a form that dynamically adds inputs depending on a user-supplied number. The form is used for booking courses, and the user selects the number of attendees, with the form adding in inputs to read in their details.

The inputs are generated and named simply by inserting the count of a FOR loop into the relevant attributes:

var inputEl = $('<input type="text" class="text delegate" name="delegate_name_' + i + '" id="delegate-name-' + i + '" placeholder="Delegate Name ' + (i + 1) + '" /><input type="text" class="text delegate" name="delegate_email_' + i + '" id="delegate-email-' + i + '" placeholder="Delegate Email ' + (i + 1) + '" /><input type="text" class="text delegate" name="delegate_tel_' + i + '" id="delegate-tel-' + i + '" placeholder="Delegate Telephone ' + (i + 1) + '" />')

This is all fine and dandy and works fine. However, I am about to write the PHP for mailing the form, and the thought occurs to me that I don't know how to tell the mail script how many inputs it needs to read.

I assume it needs another FOR loop to run through and create the relevant e-mail entries, but my knowledge of PHP is limited. Any ideas?

ElendilTheTall
  • 1,344
  • 15
  • 23
  • I would use name="delegate_name[]" instead of delegate_name_(increment) , that will create an array of delegate names – JohnnyFaldo Nov 04 '13 at 14:16

1 Answers1

2

If you give the dynamically created fields (along with pre-rendered versions of the same type) a name attribute of delegates[] then PHP will store it internally as an array.

<input type="text" name="delegates[]" id="delegate1">
<input type="text" name="delegates[]" id="delegate2">
<input type="text" name="delegates[]" id="delegate3">
<input type="text" name="delegates[]" id="delegate4">

It should then be easy to iterate over that array, performing the actions you require.

foreach ($_POST['delegates'] as $delegate) {
    ...
}

See How to define an array of form fields PHP

Specifically for the code you posted, I believe you will need to do something similar to this:

<?php
    // Set properties
    $to     = "mail@mail.com";  // Enter the email address to send email to.
    $subject    = "Booking Form Submission";            // Enter a subject for your email.

    // Set the web address to forward the user to when the mail is successfully sent.
    $url        = "success.html";

    $message = $_POST['message'];

    foreach ($_POST['delegates'] as $delegate) {
        $message .= "\r\n$delegate";
    }

    // Send the email, you don't need to change anything below this line.
    $sent = mail($to, $subject, $message, "From: " . $_POST["email"], "-f" . $_POST["email"], "Telephone:" . $_POST["tel"], "Payment Method:". $_POST["payment"], "Payment Address:" . $_POST["address"], "Purchase Order Number:" . $_POST["pono"], "No. of Delegates:" . $_POST["delno"], "Residential?:" . $_POST["residential"], "Exam?:" . $_POST["exam"], "Total Cost:" . $_POST["total_cost"]);

    // See if mail was sent
    if($sent) {
        // Email was sent successfully. Note that all we can see is that the mail was sent
        // from the server, but we cannot determine if it arrived at it's destination.
        header("Location: " . $url);
    } else {
        // Mail was not sent
        die("Your email has not been sent.");
    }
?>
Community
  • 1
  • 1
michaelward82
  • 4,706
  • 26
  • 40
  • Could you tell me how I'd integrate this into the mailer, here: http://pastebin.com/ywPvPM5Y ? – ElendilTheTall Nov 04 '13 at 14:39
  • I don't know what you're trying to do. Quite simply, inside the loop you, for each iteration you will have the delegates details from the form. If you're sending individual email then do it all inside the loop. If you're sending one email to multiple people then you may want to concatenate a string to insert in the to, cc or bcc fields - in which case the only thing inside the loop would be concatenation. – michaelward82 Nov 04 '13 at 14:51
  • I'm just looking to send the form to one person – ElendilTheTall Nov 04 '13 at 14:53
  • You need to explain what you would like to do with the data. We have an array with the delegate details in - we need to do something with that data, but I have no idea what you would like to do with it. – michaelward82 Nov 04 '13 at 14:59
  • Essentially I need to e-mail it just like any other form. The pastebin link above is the code I'm using (supplied by my ISP) to do that. – ElendilTheTall Nov 04 '13 at 15:05
  • Are you looking to add the details to the body of your email? – michaelward82 Nov 04 '13 at 15:12
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/40513/discussion-between-michaelward82-and-elendilthetall) – michaelward82 Nov 04 '13 at 15:32