Home > Mobile >  getting the value of numbered radio array name in php
getting the value of numbered radio array name in php

Time:01-12

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] : "";
    }

CodePudding user response:

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.

CodePudding user response:

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.

  • Related