How to have a unique id and then recover that id on the php page to write to the database

Asked

Viewed 96 times

1

Hello, I created these functions that create new multiple choice questions automatically on my page. I can create as many questions as I want, and in those answers I can have as many answers as I want. I want to record these answers and questions in the mysql database. What I have no idea how to do is how to get the id of each question and each answer. I couldn’t think of a logic for answers to always have an order.

Closed

<script>
    var mudadiv = 0;
        var novasperguntas = 0;
        var mudaradio = 0;
        var mudatxt = 0; 
  function PME() {

          
            var html = "";
            html += '<div class="main-login main-center">' +
                    '<div class="form-group">' + '<label for="name" class="cols-sm-2 control-label">Digite sua pergunta de multipla escolha.</label>' +
                    '<div class="cols-sm-10">' + '<div class="input-group">' + '<span class="input-group-addon">' +
                    '<i class="fa fa-user fa" aria-hidden="true">' + '</i></span>' +
                    '<input type="text" class="form-control" id="perguntaF' + novasperguntas + ' "  placeholder="Qual a sua idade?" />' + '<br>' +
                    '</div>' + '</div>' + '<div class="row">' +
                    '<div class="col-lg-6">' + '<div class="input-group">' + '<span class="input-group-addon">' +
                    '<input type="radio" id="radioperFechada' + mudaradio + ' ">' + '</span>' + '<input type="text" id="textodaresposta' + mudatxt + '-' + mudadiv + ' " class="form-control" aria-label="...">' +
                    '</div>' + '</div>' + '<div id="maisresposta2' + mudadiv + '">' + '</div>' + '</div>' + '<div id="a'+ mudadiv + '"style="display:block">'+
                    '<button onclick="MaisRespostas2(' + mudadiv + ')" style="margin-top:5px" class="btn btn-info" type="button">Nova Resposta</button>' + '</div>'+
                    '<div id="NPME">' + '</div>' + '</div>' + '</div>' + '</div>' + '</div>' + '</div>' + '</div>' + '</div>' + '';

            document.getElementById("multiplaescolha").insertAdjacentHTML('beforeend', html);
            mudatxt++;
            novasperguntas++;
           
            mudadiv++;
            mudaradio++;
            return false;
         
        }

var con = 0;
        var myradio =0;

        function MaisRespostas2(mudaduv) {
            
            var htmlresposta = "";
            htmlresposta += '<div class="col-lg-6">' + '<div class="input-group">' + '<span class="input-group-addon">' +
                    '<input type="radio" id="chequeRadio' + con + ' ">' + '</span>' +
                    '<input type="text" id="textodaresposta' + mudatxt + "-" + mudaduv + ' " " class="form-control" aria-label="...">' + '</div>' + '</div>' + '';

            document.getElementById("maisresposta2" + mudaduv + "").insertAdjacentHTML('beforeend', htmlresposta);
            mudatxt++;
            con++;
            return false;
        }
</script>

inserir a descrição da imagem aqui

1 answer

1


Ideal for Unique Id is working with Guid.

I use this function in javascript to generate my Guid.

    function createGuid() {
        function s4() {
            return Math.floor((1 + Math.random()) * 0x10000).toString(16).substring(1);
        }
        return s4() + s4() + '-' + s4() + '-' + s4() + '-' + s4() + '-' + s4() + s4() + s4();
    }

All you see is that you need a new one Id simply call the function as follows:

var seuObjeto.id = createGuid();

Browser other questions tagged

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