How to pass a Select and a Checkbox type input as dependent two-dimensional arrays through $ POST to a table?

0

I have the following form.

<!DOCTYPE html>
<html>
<head><title>ARREGLO_BID</title></head>
<body>
<form method="POST" action="pruebados.php">
    <select name="form[][txtmateria]">
        <option selected value="">SELECCIONE MATERIA</option>
        <option value="ADMINISTRACION">ADMINISTRACION</option>
        <option value="COMERCIO">COMERCIO</option>
        <option value="SISTEMAS">SISTEMAS</option>
    </select>
    <label><input name="form[][txtgrado]" value="SALA_1" type="checkbox">SALA_1</label>
    <label><input name="form[][txtgrado]" value="SALA_2" type="checkbox">SALA_2</label>
    <label><input name="form[][txtgrado]" value="SALA_3" type="checkbox">SALA_3</label>
<input type="submit" name="btnenviar" value="ENVIAR">
</form>
</body>
</html>
For the file that receives the $ POST, tested.php; I do the following to form the table:
if ( isset( $_POST['form'] ) ){
echo '<table border="1">';    
echo '<thead>';
    echo '<tr>';
        echo '<th>MATERIA</th>';
        echo '<th>GRADO</th>';
    echo '</tr>';
echo '</thead>';    
echo '<tbody>';    
foreach ( $_POST['form'] as $diam ) {
    echo '<tr>';
        echo '<td>', $diam['txtmateria'],'</td>';
        echo '<td>', $diam['txtgrado'],  '</td>';
    echo '</tr>'; }
    echo '</tbody>';
echo '</table>';}

The idea is that, for example, when selecting a subject, this will repeat the number of times of Checkbox selections in the table, if you select a subject and two Checkbox, it will appear something like this:

But I have the following errors.

According to the suggestions of Kleith , modifying the name attributes like this:

<select name="form[txtmateria]">
<input name="form[txtgrado][]" type="checkbox">

And reordering in foreach for the file tested.php and implementing a for cycle to print the array inside the foreach, like this:

if ( isset( $_POST['form'] ) ){
echo '<table border="1">';    
echo '<thead>';
    echo '<tr>';
        echo '<th>MATERIA</th>';
        echo '<th>GRADO</th>';
    echo '</tr>';
echo '</thead>';    
echo '<tbody>';    
foreach ( $_POST['form'] as $txtgrado ) {
    for($x=0;$x<count($txtgrado);$x++){
        echo '<tr>';
            echo '<td>' .$_POST['form']['txtmateria'].'</td>';            
            echo '<td>' .$txtgrado[$x].'</td>';
        echo '</tr>'; } 
    }
echo '</tbody>';
echo '</table>'; }

I get a small detail on the first line when printing (a letter "A" that I do not know where it comes from), following the example I proposed to select a subject and two rooms, which would affect me when entering a BD, like this:

    
asked by josearegu 27.12.2017 в 16:16
source

1 answer

1

To achieve this you should modify the name attributes correctly:

<!DOCTYPE html>
<html>
<head><title>ARREGLO_BID</title></head>
<body>
<form method="POST" action="pruebados.php">
    <select name="form[txtmateria]">
        <option selected value="">SELECCIONE MATERIA</option>
        <option value="ADMINISTRACION">ADMINISTRACION</option>
        <option value="COMERCIO">COMERCIO</option>
        <option value="SISTEMAS">SISTEMAS</option>
    </select>
    <label><input name="form[txtgrado][]" value="SALA_1" type="checkbox">SALA_1</label>
    <label><input name="form[txtgrado][]" value="SALA_2" type="checkbox">SALA_2</label>
    <label><input name="form[txtgrado][]" value="SALA_3" type="checkbox">SALA_3</label>
<input type="submit" name="btnenviar" value="ENVIAR">
</form>
</body>
</html>

This returns a array ordered.

array(2) {
  ["form"]=>
  array(2) {
    ["txtmateria"]=>
    string(14) "ADMINISTRACION"
    ["txtgrado"]=>
    array(1) {
      [0]=>
      string(6) "SALA_1"
    }
  }
  ["btnenviar"]=>
  string(6) "ENVIAR"
}

And in the pruebados.php :

if (isset($_POST['form'])) {
  echo '<table border="1">';    
  echo '<thead>';
    echo '<tr>';
      echo '<th>MATERIA</th>';
      echo '<th>GRADO</th>';
    echo '</tr>';
  echo '</thead>';    
  echo '<tbody>';    
  foreach ($_POST['form']['txtgrado'] as $txtgrado ) {
    echo '<tr>';
      echo '<td>' . $_POST['form']['txtmateria'] . '</td>';
      echo '<td>' . $txtgrado . '</td>';
    echo '</tr>';
  }
  echo '</tbody>';
  echo '</table>';
}
    
answered by 27.12.2017 / 16:34
source