Travers dynamically generated input array

0

I need to mount an array of a form in PHP that is dynamically generated with jquery and pass this data to the DB, I am not able to separate the forms to register in the DB.

Follow the HTML:

    <form id="boletin_cad" action="env_boletin.php" method="POST">

    <aside id="inner_form">
        <select name="aluno[]" class="selec_aluno">
            <option value="">Selecione o aluno</option>
            <?php
            $pega_alunos = $db->prepare("SELECT * FROM efcol_cadastro WHERE status_conta = ? ORDER BY nome");
            $pega_alunos->execute(array('Ativo'));
            while ($dados_alunos = $pega_alunos->fetch()) {
                echo '<option value="' . $dados_alunos['nome'] . '">' . $dados_alunos['nome'] . '</option>';
            }
            ?>            
        </select><br />
        <select name="prof[]" class="selec_aluno">

            <?php
            session_start();
            echo '<option value="' . $_SESSION['usuario'] . '">' . $_SESSION['nome'] . '</option>';
            ?>            
        </select><br />
        <select name="materia[]" class="selec_materia">
            <option value="">Selecione a Matéria</option>
            <option value="01">LIBERTAÇÃO I</option>
            <option value="02">REGIÕES DE CATIVEIRO</option>
            <option value="03">H. ESPIRITUAL</option>
            <option value="04">ESQ. JEZABEL</option>
            <option value="05">ABORTO</option>
            <option value="06">ADULTÉRIO</option>
            <option value="07">DIVÓRCIO</option>
            <option value="08">LIB. SEXUAL I</option>
            <option value="09">LIB. SEXUAL II</option>
            <option value="10">INIMIGOS ESPIRITUAIS</option>
        </select><br />
        <legend>
            <span>Ano Letivo</span>
            <input type="text" name="ano[]" class="input_ano" />
        </legend>
        <legend>
            <span>Semestre</span>
            <select name="semestre[]" class="selec_semestre">
                <option value="1">1º</option>
                <option value="2">2º</option>
            </select></legend><br /><br />
        <legend>
            <span>Média</span>
            <input type="text" name="media[]" class="input_notas"/>
        </legend>
        <legend>
            <span>Faltas</span>
            <input type="text" name="faltas[]" class="input_faltas"/>
        </legend><br /><br />
        <div class="progress">
            <div class="bar"></div >
            <div class="percent">0%</div >
        </div>
        <div id="status"></div>
    </aside>
    <div class="botoes">
        <button name="add_media">Cadastrar</button>
        <a href="javascript:void(0);" class="add_aluno">+ ADD ALUNO +</a>
        <!--<a href="javascript:void(0);" class="teste">+ TESTE +</a>-->
    </div>        
</form>

And Jquery:

$("body").on('click', 'button[name="add_media"]', function () {
    var bar = $('#boletin_cad .bar');
    var percent = $('#boletin_cad .percent');
    var status = $('#boletin_cad #status');

    $('#boletin_cad').ajaxForm({
        beforeSend: function () {
            status.empty();
            var percentVal = '0%';
            bar.width(percentVal);
            percent.html(percentVal);

        },
        uploadProgress: function (event, position, total, percentComplete) {
            var percentVal = percentComplete + '%';
            bar.width(percentVal);
            percent.html(percentVal);
            status.html('Processando aguarde...');
        },
        success: function () {
            var percentVal = '100%';
            bar.width(percentVal);
            percent.html(percentVal);
        },
        complete: function (xhr) {
            status.html(xhr.responseText);
        },
        datatype: 'json',
        url: 'env_boletin.php',
        resetForm: false
    });
});

This is the file I'm looking for the array to save to BD:

$nome = $_POST['aluno'];
$professor = $_POST['prof'];
$materia = $_POST['materia'];
$ano = $_POST['ano'];
$semestre = $_POST['semestre'];
$media = $_POST['media'];
$faltas = $_POST['faltas'];

$arr = array(
    'nome' => $nome,
    'professor' => $professor,
    'materia' => $materia,
    'ano' => $ano,
    'semestre' => $semestre,
    'media' => $media,
    'faltas' => $faltas
);

In this case, I would like to create an array that would separate form 1 from form2 ... and save it from DB already thanks.

    
asked by anonymous 01.11.2016 / 00:14

1 answer

0

Since no one answered my question in time, I'm going to contribute so that more people can be aids and not waste much time trying to solve a simple problem like that.

Follow the HTML Code:

<form id="boletin_cad" action="env_boletin.php" method="POST">
        <h2>BOLETIN DO ALUNO</h2><br />
        <aside id="inner_form">
            <select name="aluno[]" class="selec_aluno">
                <option value="">Selecione o aluno</option>
                <?php
                $pega_alunos = $db->prepare("SELECT * FROM efcol_cadastro WHERE status_conta = ? ORDER BY nome");
                $pega_alunos->execute(array('Ativo'));
                while ($dados_alunos = $pega_alunos->fetch()) {
                    echo '<option value="' . $dados_alunos['nome'] . '">' . $dados_alunos['nome'] . '</option>';
                }
                ?>            
            </select><br />
            <select name="prof[]" class="selec_aluno">

                <?php
                session_start();
                echo '<option value="' . $_SESSION['nome'] . '">' . $_SESSION['nome'] . '</option>';
                ?>            
            </select><br />
            <select name="materia[]" class="selec_materia">
                <option value="">Selecione a Matéria</option>
                <option value="01">LIBERTAÇÃO I</option>
                <option value="02">REGIÕES DE CATIVEIRO</option>
                <option value="03">H. ESPIRITUAL</option>
                <option value="04">ESQ. JEZABEL</option>
                <option value="05">ABORTO</option>
                <option value="06">ADULTÉRIO</option>
                <option value="07">DIVÓRCIO</option>
                <option value="08">LIB. SEXUAL I</option>
                <option value="09">LIB. SEXUAL II</option>
                <option value="10">INIMIGOS ESPIRITUAIS</option>
            </select><br />
            <legend>
                <span>Ano Letivo</span>
                <input type="text" name="ano[]" class="input_ano" />
            </legend>
            <legend>
                <span>Semestre</span>
                <select name="semestre[]" class="selec_semestre">
                    <option value="1">1º</option>
                    <option value="2">2º</option>
                </select></legend><br /><br />
            <legend>
                <span>Média</span>
                <input type="text" name="media[]" class="input_notas"/>
            </legend>
            <legend>
                <span>Faltas</span>
                <input type="text" name="faltas[]" class="input_faltas"/>
            </legend>            
        </aside>
        <div class="botoes">
            <button name="add_media">Cadastrar</button>
            <a href="javascript:void(0);" class="add_aluno">+ ADD ALUNO +</a>
            <a href="javascript:void(0);" class="ver_boletins">+ BOLETINS +</a>
            <div class="progress">
                <div class="bar"></div >
                <div class="percent">0%</div >
            </div>
            <div id="status"></div>
        </div>        
    </form>

The form above is dynamically generated so your input and select name are bracketed to retrieve as an array in php.

follows PHP:

include '../conecta.php';
$db = con::conectar();

$arr = array(
    'nome' => $_POST['aluno'],
    'professor' => $_POST['prof'],
    'materia' => $_POST['materia'],
    'ano' => $_POST['ano'],
    'semestre' => $_POST['semestre'],
    'media' => $_POST['media'],
    'faltas' => $_POST['faltas']
);

for ($i = 0; $i < count($arr['nome']); $i++) {
    $nameAluno = $arr['nome'][$i];
    $nameProf = $arr['professor'][$i];
    $nameMateria = $arr['materia'][$i];
    $anoLetivo = $arr['ano'][$i];
    $periodo = $arr['semestre'][$i];
    $valorMedia = $arr['media'][$i];
    $numFaltas = $arr['faltas'][$i];

    switch ($nameMateria) {
        case '01': $materiaNome = 'Libertação I - Padrão de aconselhamento na libertação';
            break;
        case '02': $materiaNome = 'Regiões de Cativeiro';
            break;
        case '03': $materiaNome = 'H. Espiritual';
            break;
        case '04': $materiaNome = 'Esq. Jezabel';
            break;
        case '05': $materiaNome = 'Aborto';
            break;
        case '06': $materiaNome = 'Adultério';
            break;
        case '07': $materiaNome = 'Divórcio';
            break;
        case '08': $materiaNome = 'Libertação Sexual I';
            break;
        case '09': $materiaNome = 'Libertação Sexual II';
            break;
        case '10': $materiaNome = 'Inimigos Espirituais';
            break;
    }

    switch ($periodo) {
        case '1': $periodoValue = '1º';
            break;
        case '2': $periodoValue = '2º';
            break;        
    }

    $includeBanco = $db->prepare("INSERT INTO media_alunos (aluno,professor,materia,ano,semestre,media,faltas) VALUES (?,?,?,?,?,?,?)");
    $includeBanco->execute(array($nameAluno, $nameProf, $materiaNome, $anoLetivo, $periodoValue, $valorMedia, $numFaltas));
}

echo '<pre>Cadastro efetuado com sucesso.</pre>';

All I had to do in PHP was to retrieve the variables of each input and select respectively and at the time of sorting the arrays I just had to do the count on the "for" with a key of the variables only and the rest I would pick up of the loop and the variable of increment, and ready solved, I was able to save in the pack each form separately with their respective data.

    
02.11.2016 / 12:36