Reputation: 27
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'
?
Upvotes: 0
Views: 28
Reputation: 107
Either use $_POST['sample'.$i] or use sample[] as name attribute & iterate over that.
Upvotes: 0
Reputation: 2962
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.
Upvotes: 1
Reputation: 36964
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.
Upvotes: 0