Wilest
Wilest

Reputation: 1860

passing checkbox & text field value in php

I'm trying to pass a text value field over to the next page for every checkbox selected, but I'm only getting the last text fields value, example:

checkbox textfield
selected ABCD
selected ABCDE

I am only getting back the ABCDE every time

page1.php

echo "<td width='10px'><input name='question[$rowid][]' type='checkbox' value='1' /></td>";
echo "<td width='230px'><input name='newname' type='text' value='$certn'/></td>";

page2.php

foreach ($_POST['question'] as $key => $ans) {
$nn = $_POST['newname'];
echo $key . $nn;
echo "</br>";
}

Help will be greatly appreciated

Upvotes: 0

Views: 828

Answers (2)

DaveRandom
DaveRandom

Reputation: 88647

It's a little hard to work out exactly what you're doing here but I think your statement I'm only getting the last text fields value indicates your problem - you have multiple fields with the same name. If you do this and don't make them into an array ([]), you will only get the last value on the page.

I think you want something more like this:

Page 1:

echo "<td width='10px'><input name='question[$rowid]' type='checkbox' value='1' /></td>";
echo "<td width='230px'><input name='newname[$rowid]' type='text' value='$certn'/></td>";

Page 2:

foreach ($_POST['question'] as $key => $ans) {
  // $_POST['newname'] is now also an array, and the keys should correspond to
  // those in the $_POST['question'] array
  $nn = $_POST['newname'][$key];
  echo $key . $nn;
  echo "</br>";
}

Upvotes: 2

felixgaal
felixgaal

Reputation: 2423

The line:

echo "<td width='10px'><input name='question[$rowid][]' type='checkbox' value='1' /></td>";

will not be correctly interpreted. You must change it to:

echo "<td width='10px'><input name='" . $question[$rowid][] . "' type='checkbox' value='1' /></td>";

Arrays are not substitued inside a string.

Upvotes: 0

Related Questions