Save dynamic form data

0

The form is created through data obtained from the MySQL database. I would like to save all the data from it by clicking submit.

Problem:

  • I can only save one line at a time by creating a button in the same <tr> and with the aid of javascript parent ('tr');

  • When saving the value of each of the generated text boxes dynamically, I can not write the form completely, that is, what I want.



  • Form PHP Code

    <?php
    include('conetar.php');
    $result = mysqli_query($conn,"SELECT * FROM 'opcoes' WHERE 'tipo' = 1 ;");
    
    
    echo "<table class='table table-striped table-hover'id='datatables-example'>
    <tr>
    </tr>
    <tr>
    <td></td>
    <td class='pure-table'><b>Antes da Instalação do Sistema Operativo</b></td>
    <td class='pure-table'><b>Verificação</b></td>
    </tr>";
    
    
    while($row = mysqli_fetch_array($result)) 
    {
    
    echo "<tbody data-link='row' class='rowlink'>";
    echo "<tr>";
    echo "<td> <input type='hidden' name='IDOpcao' value=".$row['IDOpcao']."></td>";
    echo "<td>" . $row['Opcao'] . "</td>";
    echo "<td  style='text-align:center;'>  <input type='checkbox' name='Validacao'></td>";
    echo "</tr>";
    echo "</tbody>";    
    echo "<input type='hidden' name='IDOpcao' class='form-control' value='". $row['IDOpcao'] . "'>";
    }
    echo "</table>";
    mysqli_close($conn);
    ?>
    
    
    
    
    <br>
    
    <?php
    include('conetar.php');
    $result = mysqli_query($conn,"SELECT * FROM 'opcoes' WHERE 'tipo' = 2 ;");
    
    echo "<table class='table table-striped table-hover'id='datatables-example'>
    
    <tr>
    <td></td>
    <td class='pure-table'><b>Instalação e Configuração (Base)</b></td>
    <td class='pure-table'><b>Versão Antiga</b></td>
    <td class='pure-table'><b>Versão Nova</b></td>
    <td class='pure-table'><b>Verificação Prévia</b></td>
    
    </tr>";
    
    while($row = mysqli_fetch_array($result)) 
    {
    
    echo "<tbody data-link='row' class='rowlink'>";
    echo "<tr>";
    echo "<td> <input type='hidden' name='IDOpcao' value=".$row['IDOpcao']."></td>";
    echo "<td>" . $row['Opcao'] . "</td>";
    echo "<td>  <input type='text' name='VersaoAntiga' id= 'VersaoAntiga-".$row['IDOpcao']."' class='form-control versao_antiga'></td>"; 
    echo "<td>  <input type='text' id='VersaoNova-".$row['IDOpcao']."' name='VersaoNova' class='form-control' disabled='true'></td>";
    echo "<td  style='text-align:center;'><input type='checkbox' name= 'VerificacaoPrevia'></td>";
    echo "</tr>";
    echo "</tbody>";    
    echo "<input type='hidden' name='IDOpcao' class='form-control' value='". $row['IDOpcao'] . "'>";
    }
    
    echo "</table>";
    mysqli_close($conn);
    
    ?>
    <!--Script para copiar para um ficheiro-->
    
    <script>
    
    $(document).ready(function(e){
    $(".versao_antiga").on('input', function(e) {
    var elemento = $(this);
    var idElemento = elemento.attr("id").split('-')[1];
    
    $("#VersaoNova-" + idElemento).prop('disabled', elemento.val().length == 0);
    });
    });
    </script>
    <br>
    <!--Script para copiar para um ficheiro-->
    
    <?php
    include('conetar.php');
    $result = mysqli_query($conn,"SELECT * FROM 'opcoes' WHERE 'tipo' = 3 ;");
    
    echo "<table class='table table-striped table-hover'id='datatables-example'>
    <tr>
    <td></td>
    <td class='pure-table'><b>Instalação e Configuração (Complementar)</b></td>
    <td class='pure-table'><b>Versão Antiga</b></td>
    <td class='pure-table'><b>Versão Nova</b></td>
    <td class='pure-table'><b>Verificação Prévia</b></td>
    
    
    </tr>";
    
    while($row = mysqli_fetch_array($result)) 
    {
    
    echo "<tbody data-link='row' class='rowlink'>";
    echo "<tr>";
    echo "<td>   <input type='hidden' name='IDOpcao' value=".$row['IDOpcao']."></td>"; 
    echo "<td>" . $row['Opcao'] . "</td>";
    echo "<td>  <input type='text' name='VersaoAntiga' id= 'VersaoAntiga-".$row['IDOpcao']."' class='form-control versao_antiga'></td>"; 
    echo "<td>  <input type='text' id='VersaoNova-".$row['IDOpcao']."' name='VersaoNova' class='form-control' disabled='true'></td>";
    echo "<td  style='text-align:center;'><input type='checkbox' name= 'VerificacaoPrevia' ></td>";
    
    echo "</tr>";
    echo "</tbody>";    
    echo "<input type='hidden' name='IDOpcao' class='form-control' value='". $row['IDOpcao'] . "'>";
    }
    
    echo "</table>";
    mysqli_close($conn);
    
    ?>
    

    Form Image

        
    asked by anonymous 15.11.2016 / 16:28

    1 answer

    0

    Friend first try working with OO + PDO. Create a DAO class and write your CRUD there and you will have better control and result ... I would in your case create two methods insert example:

     public function cadastrar(Ficha $ficha1)
    {
    
        $this->ficha = $ficha1;
    
    
        //EXTRAINDO OBJETO PARA VARIAVEL
        $nome = $this->ficha->getUser();
        $professor = $this->ficha->getProfessorAvaliado();
        $turma = $this->ficha->getTurma();
        $campus = $this->ficha->getCampus();
        $disciplina = $this->ficha->getDisciplina();
        $nivel = $this->ficha->getNivel();
        $questa1 = $this->ficha->getQ1();
        $questa2 = $this->ficha->getQ2();
        $questa3 = $this->ficha->getQ3();
        $questa4 = $this->ficha->getQ4();
        $questa5 = $this->ficha->getQ5();
        $questa6 = $this->ficha->getQ6();
        $parecer = $this->ficha->getParecer();
    
    
    
    
        //--------------------------------------
        try{
    
            $stmt = $this->pdo->prepare("INSERT INTO ficha(user,professorAvaliado,turma,campus,disciplina,nivel,q1,q2,q3,q4,q5,q6,parecer) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?)");
            $stmt->bindParam(1,$nome);
            $stmt->bindParam(2,$professor);
            $stmt->bindParam(3,$turma);
            $stmt->bindParam(4,$campus);
            $stmt->bindParam(5,$disciplina);
            $stmt->bindParam(6,$nivel);
            $stmt->bindParam(7,$questa1);
            $stmt->bindParam(8,$questa2);
            $stmt->bindParam(9,$questa3);
            $stmt->bindParam(10,$questa4);
            $stmt->bindParam(11,$questa5);
            $stmt->bindParam(12,$questa6);
            $stmt->bindParam(13,$parecer);
    
            $resultado = $stmt->execute();
    
            if ($resultado){
                echo "dados inseridos com sucesso";
            }
    
    
        }  catch (PDOException $e) {
            echo $e->getMessage();
        }
    }
    

    To learn more about NxN relationships link

        
    08.05.2017 / 14:09