1

I made a form in which the user can clone/duplicate a input field with JQuery for adding more informations. The problem is just the last cloned input is available in the sent mail.

I tried with PHP something like this:

$product = "";
$product.= $_POST['product'] .= "\t amount".$trenner.$_POST['amount'] .= "\t price".$trenner.$_POST['price']."€";

But it doesn't work.

Jquery:

var products = $(".products").clone();
var clonedRow = products.clone().insertAfter(".products:last").find(":input").val(this.value ).end();
//ajax
console.log(products,clonedRow,$('[name="Product"]', clonedRow))
Jay Blanchard
  • 34,243
  • 16
  • 77
  • 119
  • 1
    Your input name has to be `amount[]`, then you can get values in PHP as `array`. – Rene Korss Oct 14 '15 at 12:44
  • Thanks for anwser @ReneKorss. You mean like that: and $product.= "\t amount".$trenner.$_POST['amount[]'] – user5441400 Oct 14 '15 at 13:47
  • Seems isn´t working for me like that. – user5441400 Oct 14 '15 at 13:59
  • Side issue: By directly interpolating user-supplied values into your HTML output (`$product.= $_POST['product']`...) you are opening yourself up to various security problems. [This](http://stackoverflow.com/questions/1996122/how-to-prevent-xss-with-html-php) is a good place to start. – Palpatim Oct 14 '15 at 14:12

1 Answers1

1

First, your input name should have [] in end of it, so it can have multiple values.

<input type="text" name="amount[]" placeholder="amount" required />

In PHP, you will get it like usual:

$amounts = $_POST['amount']; // array

but now it is array, so you can't concatenate it like regular value. You could, for example join values together separated with comma ,.

This can be done with implode.

$amounts = implode(', ', $_POST['amount']);
$product.= $_POST['product'] .= "\t amount".$trenner.$amounts .= "\t price".$trenner.$_POST['price']."€";

Point here is that you must use array values and generate some string out of it.

If you want to sum them together, you can use array_sum.

$sum = array_sum($_POST['amount']);
Rene Korss
  • 5,414
  • 3
  • 31
  • 38