Store dynamic form data

Asked

Viewed 281 times

0

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

Problem:

  1. I can only keep one line at a time, creating a boot in it <tr> and with the help of javascript Parent('tr');

  2. When saving the value of each of the generated text boxes dynamically, I cannot record the form completely, that is, what I want.



PHP Code of the Form

<?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

inserir a descrição da imagem aqui

1 answer

0

Friend first try to work with OO + PDO. Create a DAO class and write there your CRUD and will have better control and result... I in your case would 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 http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/association-mapping.html

  • I’m creating a simple form, I don’t think it makes sense to use a MVC framwork for this case.I can record if it’s line by line using jquery with parent('tr'), but, I would like to store everything while submit.

  • @djva Of course it makes sense to use PDO... and not MVC Framework. PDO is not a MVC Framework it only replaces MSQLI (which is depreciating). The MVC OO methodology etc helps you precisely with this, in solving problems... It is recommended that any system as long as a simple form follows the OO standards with Bean classes, DAO, etc... How long have you been working with PHP ?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.