Home » Php » php – How to get this name with id after click submit button?

php – How to get this name with id after click submit button?

Posted by: admin February 25, 2020 Leave a comment


The below code I use input name result+row id and Its working as expected.

<td class="t-width-150" id="result">
   <input type="text" name="result<?php echo $row['id']; ?>" id="txtresult" value="0">

Now I want to get this name with id in another page after clicking submit button. What should I use in below code:

<td class="t-width-150">
  <?php echo $_POST['result']; ?>

Thank you!

Update: I solved it. Don’t need more answers. Thank you, everyone!

How to&Answers:

You can use a form field naming format like this:

name="result[<?php echo $row['id']; ?>]"

So effectively, this will result in field names like result[1], result[2], result[5] (the ids do not need to be consecutive or in order for this.)

PHP will then generate an array out of this in $_POST['result'], that you can loop over using the extended foreach syntax, to get access to both your row id and the submitted value:

foreach( $_POST['result'] as $id => $value ) {
  echo "The submitted value for the record with ID ", $id, " was ", $value, "<br>\n";


Are you saying that you don’t know in advance what POST variables there will be, because there is an unknown number of results, and therefore an unknown number of textboxes, in the form? If so then you could either

a) Loop through all the $_POST variables to find any which start with “result” and then extract the value from it e.g.

foreach (array_keys($_POST) as $key)
  if (preg_match("/result(\d+)/", $key, $matches))
    echo $_POST[$key]; //will get the value entered in the input field
    echo $matches[1]; //will get you the row ID from the field name e.g. from "result123" it would return "123".


b) use array-syntax in the form field names, and have the result and ID fields separate e.g.

<input type="text" name="result[]" id="txtresult" value="0" />
<input type="hidden" name="rowID[]" value="<?php echo $row['id']; ?>"/>

and then in PHP you’d loop through $_POST["result"] and then examine the matching index in $_POST["rowID"], which will be arrays, to relate the result together.