How to have a unique id and then retrieve this id in php page to write to the bank

1

Hello, I created these functions that automatically create new multiple-choice questions on my page. I can create as many questions as I want, and in these answers I can have as many answers as I want. I want to write these answers and questions in the mysql database. What I have no idea how to do is how I get the id of each question and every answer. I could not think of a logic for the 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>

    
asked by anonymous 12.03.2018 / 18:01

1 answer

1

The Ideal for Single Id is to work 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 need is a new Id just call the function as follows:

var seuObjeto.id = createGuid();
    
12.03.2018 / 18:06