0

I have a form with inputs for 'name' and 'email'. And another for 'data-1'. The user can click on add button and jQuery will dynamically add a input for 'data-2', 'data-3' etc..as needed.

The form is posted to a PHP emailer script which validates fields and places data into a template for mailing.

How can i add inputs 'data-2', 'data-3' etc.. if they are created? And if they are not how can i avoid gaps in my email template?

(is there a way to write it so if the post is received add this and if not do nothing?)

Here is an example of the code i am using:

$name = $_POST['name'];
$email = $_POST['email'];
$data-1 = $_POST['data-1'];

(do i need to add: $data-2 = $_POST['data-2'] and $data-3....up to a set value of say 10?)

$e_body = "You were contacted by $name today.\r\n\n";
$e_data = "Data Set 1: $data-1.\r\n\n"; 

Here is where i would like to show "Data Set 2/3/4....etc" if they exist

$e_reply = "You can contact $name via email, $email";

$msg = $e_body . $e_data . $e_reply;

if(mail($address, $e_subject, $msg, "From: $email\r\nReply-To: $email\r\nReturn-Path: $email\r\n")) {

I hope that is clear and thank you for any help or guidance

thom

thom
  • 1,306
  • 4
  • 12
  • 15

3 Answers3

2

You should be using input arrays for this purpose.

In your HTML, set the name of the form element to conform to this naming scheme: data[].

Then, when the form is submitted you can simply loop through this array and add fields to the email within the loop:

$name = $_POST['name'];
$email = $_POST['email'];
$data = $_POST['data'];
$e_data = '';

foreach($data as $i => $d) {
   if((int) $i != $i) continue; //thanks Alex
   $e_data .= "Data Set {$i}: " . $d . "\r\n\n";
}

//...

On the client side, your code should be something like this:

<input type="hidden" name="data[]"/>
Jacob Relkin
  • 161,348
  • 33
  • 346
  • 320
  • 1
    If the array being passed in isn't an associative array, you can use `foreach($data as $i => $field)`. That said, it doesn't mean that a, potentially malicious, user can't pass in an associative array. Of course, one can guard against this. – George Marian Jul 21 '10 at 01:16
  • I think I've always used `foreach` with a `if ((int) $key != $key) continue;` but yeah I suppose it doesn't matter either way. Even with the `for` loop, however, one could send a non numerical key (and cause problems with the loop). Edit: I got rid of `is_int()` because I *think* PHP gets those keys as strings by default. – alex Jul 21 '10 at 01:28
  • hi thanks for reply, unfortuantely i am getting this error: Parse error: syntax error, unexpected T_IF in error refers to line starting with if((int) am i doing something wrong? – thom Jul 21 '10 at 05:19
  • @thom, sorry i had a typo in there. Try again with my revised code. – Jacob Relkin Jul 21 '10 at 05:26
  • thanks again, i tried the new code and the script passes but it does not input the data. just name and email – thom Jul 21 '10 at 05:42
  • @thom, did you make sure to append `[]` to your input `name`s in your HTML? – Jacob Relkin Jul 21 '10 at 05:52
  • yes i appended [] - sorry if your are looking at the code and its a bit messy and more complicated than original question. thank you so much for your help – thom Jul 21 '10 at 05:55
  • hang on was it supposed to be [ ] or [] - difference being space between -- does that make a difference? – thom Jul 21 '10 at 05:57
  • @thom, There's no difference as far as I know. Hit me up on AIM. It's a lowercase version of my name joined together. – Jacob Relkin Jul 21 '10 at 06:01
  • @thom, I can't do it right now though. I've gotta go to sleep. I'm on the US east coast, it's 2am now. I'll be back online at ~10am tomorrow. – Jacob Relkin Jul 21 '10 at 06:10
0

Use something like this:

if(isset($_POST['data-1']))
{
    // $_POST['data-1'] exists, include it.
}
else
{
    // $_POST['data-1'] doesn't exists, don't include it.
}
Daniel
  • 30,896
  • 18
  • 85
  • 139
0

As a general point form processing is trick and has many traps for the unwary. It's worth having a look at this post on best practice form processing

The crux of your problem is that you do not know how many "data" values you will get in your $_POST array. The answer is simply to iterate over the $_POST array to find your data values.

$rawData = array();
foreach ($_POST as $index => $value) {
// Test for a "data-n" index
    if ((preg_match('#^data-\d+#i', $index, $matches))) {
        $rawData[] = $value;
    }
}

The above code will copy all the $_POST values with keys of the form 'data-0', 'data-1', etc. You can then filter and validate these values.

All we do is iterate over $_POST to get each key and value. We then test the key using preg_match to see if it starts with the string 'data-' and is followed by one or more digits. If so, we add it to our raw (unfiltered, non validated) data.

In this example, we could replace the preg_match with the strpos function -

if ( strpos  ($index, 'data-') === 0) {

The use of the preg_match gives us more flexibility. For example, if you wanted to capture the numeric portion of your 'data-n' keys - 'data-23', 'data-999', 'data-5', etc. Then change the if statement to

if ((preg_match('#^data-(\d+)#i', $index, $matches))) {
    $rawData[$matches[1]] = $value;
}

The variable $matches is an array that captures the results of the search. The complete matching string is is $matches[0]. However, we have enclosed the digit matching pattern in parenthesis and hence the captured digits are placed into $matches1 which we then use to key the $rawData array. In this case $rawData would have the keys = 23, 999 and 5.

Community
  • 1
  • 1
  • thank you for replying, this is going over my head a bit, will take a while to try and understand it, this is my first time using php really – thom Jul 21 '10 at 10:36