How to store the date and time with each click on the form

1

I have a form and need to store the date and time with each click of the form or at least the first one. I think this should be done via JQuery or JavaScript. With each question, the form does have a button with type="button" and the last button does the submit. The idea is to get the beginning and end of the form filling and / or how long it takes between one question and another.

Thank you.

    
asked by anonymous 09.07.2014 / 21:57

2 answers

1

If I understand you correctly, you have a multi-step split form. And there is a button responsible for transitioning the steps. Assuming there is a button for each step of the form, you could do it as follows:

html

<form>
    <div class="etapa">
        ...
        <button type="button" name="etapa1">Avançar</div>
    </div>
    <div class="etapa">
        ...
        <button type="button" name="etapa2">Avançar</div>
    </div>
    <div class="etapa">
        ...
        <button type="button" name="etapa3">Avançar</div>
    </div>
</form>


javascript

jQuery(function(){

    jQuery("div.etapa button").click(function(){

        var dados = {};
            dados.etapa = jQuery(this).attr('name');

        jQuery.ajax({
            url  : 'logEtapas.php',
            data : dados,
            type : 'post'
        });

    });

});


php

if ( $_POST ){

    $etapa    = $_POST["etapa"];
    $datahora = date("Y-m-d H:i:s");

    //Executar inserção dos dados no banco

}
    
25.07.2014 / 13:17
0

I do not know if it's the right way, but I'd do it like this:

html

<form>
    <div class="etapa">
        <input type="hidden" name="cod_pergunta" value="1" />
        <input type="hidden" name="cod_seq_questionario" value="1" />
        Pergunta nro 1
        <select name="resposta">
            <option value="0">0</option>
            <option value="1">1</option>
            <option value="2">2</option>
            <option value="3">3</option>
            <option value="4">4</option>
            <option value="5">5</option>
        </select>
        <button type="submit">Avançar</div>
    </div>
</form>

<form>
    <div class="etapa">
        <input type="hidden" name="cod_pergunta" value="2" />
        <input type="hidden" name="cod_seq_questionario" value="2" />
        Pergunta nro 2
        <select name="resposta">
            <option value="0">0</option>
            <option value="1">1</option>
            <option value="2">2</option>
            <option value="3">3</option>
            <option value="4">4</option>
            <option value="5">5</option>
        </select>
        <button type="submit">Avançar</div>
    </div>
</form>

<form>
    <div class="etapa">
        <input type="hidden" name="cod_pergunta" value="3" />
        <input type="hidden" name="cod_seq_questionario" value="3" />
        Pergunta nro 3
        <select name="resposta">
            <option value="0">0</option>
            <option value="1">1</option>
            <option value="2">2</option>
            <option value="3">3</option>
            <option value="4">4</option>
            <option value="5">5</option>
        </select>
        <button type="submit">Avançar</div>
    </div>
</form>


javascript

jQuery(function(){

    jQuery("form").submit(function(){

        var dados = jQuery(this).serialize();

        jQuery.ajax({
            url  : 'salvar_respostas.php',
            data : dados,
            type : 'post'
        });

    });

});


php

if ( $_POST ){

    $agora = date("Y-m-d H:i:s");
    $strSQL = "INSERT INTO pv_resposta (cod_formulario, cod_pergunta, resposta, cod_seq_questionario, time_click) VALUES ('3', '" . $_POST['cod_pergunta'] . "', '" . $_POST['resposta'] . "', '" . $_POST['cod_seq_questionario'] . "', '" . agora . "');"

    //Executar inserção dos dados no banco

}
    
28.07.2014 / 12:26