0

I am using a simple method of cloning rows on a form. You can see the webpage here.

This is the script I am using to do the cloning:

$(document).ready(function() {

            $(".add").click(function() {
                $("form > p:first-child").clone(true).insertBefore("form > p:last-child");
                return false;
            });

            $(".remove").click(function() {
                $(this).parent().remove();
            });

        });

and here is the form html:

    <form method="post" action="bookingengine.php">
        <p>
            <label>Full Name:</label> <input type="text" name="name" id="name">
            <label>Email:</label> <input type="text" name="email" id="email">
            <label>Telephone:</label> <input type="text" name="telephone" id="telephone">
            <span class="remove">Remove</span>
        </p>
        <p>
            <span class="add">Add fields</span><br /><br /><input type="submit" name="submit" id="submit" value="Submit" class="submit-button" />
        </p>

    </form>

There are two issues that I am having with this, which make me wonder whether cloning is the best method:

  1. If the user has already entered information into the text boxes, then these are cloned along with the text boxes themselves, and I don't want this to happen. I would like add rows added to be empty.

  2. The information is to be submitted to an email address using PHP. Here is the PHP:

$EmailFrom = ""; $EmailTo = ""; $Subject = "Booking on Retreat"; $Name = Trim(stripslashes($_POST['name'])); $Email = Trim(stripslashes($_POST['email'])); $Telephone = Trim(stripslashes($_POST['telephone']));

$validationOK=true; if (!$validationOK) { print ""; exit; }

$Body = "New bookings have been made for the Retreat as follows:"; $Body .= "\n"; $Body .= "\n"; $Body .= "name: "; $Body .= $Name; $Body .= "\n"; $Body .= "\n"; $Body .= "email: "; $Body .= $Email; $Body .= "\n"; $Body .= "\n"; $Body .= "telephone: "; $Body .= $Telephone; $Body .= "\n";

$success = mail($EmailTo, $Subject, $Body, "From: ");

if ($success){ print ""; } else{ print ""; } ?>

What I am seeing is that only the last row in the form is being added to the email, presumably because there are not unique names or IDs for each textbox. So again I am wondering if the clone method is best suited to this task, and if it is, how I can alter my code to include all rows in the email.

Thanks,

Nick

Nick
  • 4,304
  • 15
  • 69
  • 108

1 Answers1

0

To work with the PHP, you need to convert the inputs into 'arrays', by appending the 2-character sequence '[]' to the end of the input's name (see #3 on http://php.net/faq.html for more detail).

As for cloning the objects with the values, it should be pretty simple to clear out the values in the newest added inputs as a last step of $(".add")'s 'click' handler.

    $(document).ready(function() {

        $(".add").click(function() {
            var x = $("form > p:first-child").clone(true).insertBefore("form > p:last-child");
            x.find('input').each(function() { this.value = ''; });
            return false;
        });

        $(".remove").click(function() {
            $(this).parent().remove();
        });

    });
TML
  • 12,813
  • 3
  • 38
  • 45
  • Thanks TML. The amended script for clearing the values works great. I am still not clear on the arrays though. I have added `[]` to the end of each input name, but when forms with multiple rows are submitted I am still just getting one row of data. Do I need to make adjustment to the PHP to get it working with the arrays, and if so, what amendments do I need to make? – Nick May 15 '11 at 09:56
  • You can read the documentation link I posted above - basically, each $_POST['whatever'] becomes an array of values now, instead of being the actual scalar values. So you'd read $_POST['name'][0] to get the contents of the first "name" input, $_POST['name'][1] to get the second, and so on. – TML May 15 '11 at 10:00
  • OK. Thanks, I have that working now if I add a `$_POST['name'][0,1...]` for each input in each possible row of the form. I am wondering if there is a way of constructing the PHP so that I wouldn't have to add `$_POST['name'][0,1...]` for each possible row though. In this way the body of the email would be constructed with only the necessary number of rows, and wouldn't include lots of empty redundant rows. – Nick May 15 '11 at 10:21
  • I decided to mark this question as answered as it has effectively been answered, even though there is the issue with how the email is constructed, as per my last comment. I have now asked a specific question about the outstanding issue [here](http://stackoverflow.com/questions/6009788/converting-php-script-for-generating-email-to-work-with-variable-rows-in-form) – Nick May 15 '11 at 16:48