skip to Main Content

I am building an application with php

The form is generated using for loop statement

How do I get the value of each row?

HTML

<form action="submitchecklist.php" method="post">
  <table border="2">
  <thead>
      <th colspan="2">PLUMBING</th>  
  </thead>
<tbody>
    <tr>
        <td></td>
        <td>
            Leakage
        </td>
        <td>Heater</td>
    </tr>

    <?php
    for ($i = 201; $i <= 215; $i++) {
        echo '
            <tr>
            <td>' . $i . '</td>
            <td>
                <input type="radio" value="yes" name="leak' . $i . '_[]" id="">Yes
                <input type="radio" value="no" name="leak' . $i . '_[]" id="">No
            </td>
            <td>
               <input type="radio" value="yes" name="heat' . $i . '_[]" id="">Yes
                <input type="radio" value="no" name="heat' . $i . '_[]" id="">No
            </td>
            </tr>
            '
    } ?>

</tbody>
</table>
</form>

I want to get the value of each row from the form

submitchecklist.php

 <?php

    foreach ($_POST['leak'] as $key => $value) {
        echo "<br />";
        echo $value;
        echo (isset($heat[$key])) ? $leak_no[$key] : "";
    }

2

Answers


  1. You could do it this way:

    for ($i = 201; $i <= 215; $i++) {
        $leak_value = $_POST['leak'.$i.'_[]'];
        $heat_value = $_POST['heat'.$i.'_[]'];
        echo $leak_value;
        echo $heat_value;
    }
    

    But this leak' . $i . '_[] is not very good way to organise your names.

    Login or Signup to reply.
  2. You should alter the name of the radio inputs:

    <input type="radio" value="yes" name="leak[' . $i . ']" id="">
    

    This way your originally code in submitchecklist.php works.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search