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

Asked

Viewed 926 times

1

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

Thank you.

2 answers

1

If I understand correctly, you have a form divided into several steps. And there is a button responsible for carrying out the transition of 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

}
  • Thanks for the force, @Thiago. You understood well, but this php will stay in the file that will receive the Post? If yes, how will I get the date and time at the time of the click if it will be done later? Sorry, but I didn’t quite understand the placement of jquery and php. Thanks.

  • In fact, every time the 'Next' buttons receive the click, the request will be made to the page logEtapas.php (for example) which will only insert the date/time of the click and the name of the button (only to identify the passages). In the last step there should be a button type="submit" that will send the data to the page responsible for entering the form data in the database. Another option would be to each save the information every step forward. only that ai would have to change the above scripts.

  • There’s a problem, Thiago. This way you are talking, will store information every time the user clicks forward and I will have to change the structure of my table. I created a column called time_click for each click goes forward and at the end, you should take the post of each question and the time of each click.

  • Put the script of your table so I understand better?

  • 'CREATE TABLE giga.pv_resposta(&#xA;seq int(10) unsigned NOT NULL AUTO_INCREMENT,&#xA;cod_formulario int(10) unsigned NOT NULL DEFAULT '1',&#xA;cod_pergunta int(10) unsigned NOT NULL,&#xA;resposta varchar(100) DEFAULT NULL,&#xA;cod_seq_questionario int(10) unsigned NOT NULL,&#xA;time_click varchar(45) DEFAULT NULL,&#xA;PRIMARY KEY(seq),&#xA;KEY FK_Formulario_Padrao (cod_formulario,cod_pergunta) USING BTREE,&#xA; CONSTRAINT FK_Formulario_Padrao FOREIGN KEY (cod_formulario, cod_pergunta) REFERENCES pv_formulario_padrao (cod_formulario, cod_pergunta)&#xA;) ENGINE=InnoDB AUTO_INCREMENT=199 DEFAULT CHARSET=latin1 ROW_FORMAT=DYNAMIC;'

  • The insertion is done by Stored Procedure. Thank you all.

Show 1 more comment

0

I don’t know if it’s the right way, but I’d do it this way:

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

}
  • It makes sense, @Thiago. I will test and as it is, I mark as ok. Thanks.

Browser other questions tagged

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