0

my question:

<input type="text" name="sample1">
<input type="text" name="sample2">

this input add with javascript with limit 10 max = sample10.

how to handle this with php? like $_POST['sample$i'].

for ($i = 1; $i <= 10; $i++) {
}

and if it can be done, $some$i = $_POST['sample$i'].

can i insert to db with '$some1', '$some2'?

nomistic
  • 2,902
  • 4
  • 20
  • 36
astrid
  • 27
  • 7
  • 1
    Better to just use `name="sample[]"` several times, no keys needed, then `$_POST['sample']` will be an array, much easier to deal with. – rjdown May 22 '15 at 23:54

3 Answers3

1

it's a lot easier to do it this way. In your form. set this up as:

<input type="text" name="sample[]" />

and then in your php use an array like so:

foreach $_POST['sample'] as $sample {
  echo $sample.'<br/>';

}

This way you don't have to be limited to a finite number of rows.

nomistic
  • 2,902
  • 4
  • 20
  • 36
0

Use $_POST["sample$i"]. See What is the difference between single-quoted and double-quoted strings in PHP?

Yeah, you can use ${"some$i"} = $_POST["sample$i"]; but PLEASE, don't. Use an array to do the job.

Community
  • 1
  • 1
Federkun
  • 36,084
  • 8
  • 78
  • 90
0

Either use $_POST['sample'.$i] or use sample[] as name attribute & iterate over that.