Save in database, radio input created with javascript?

0

Good in a form created a button that creates radio input with javascript, the question is that I would like to save those radio inputs (alternatives) in my database.

<form action="pregunta-quest.php" method="post">
<input type="text" id="txopcion" placeholder="Escribe Opción">
  <button type="button" onclick="adiradio()">agregar</button><br>   
  <span id="adicionarR"></span>
</form>

This is the script

<script>
function adiradio() {
            var text = document.getElementById("txopcion").value;//obtiene el text para la opcion
            if(text!=''){ //que no se pueda poner opciones vacias
            var node=document.createElement("P");//etiqueta p
            var x = document.createElement("INPUT");//crea un input
            var textnode = document.createTextNode(text);//asigna el text 
            x.setAttribute("type", "radio");//hace que type del input sea radio
            x.setAttribute("name","alternati");
            x.setAttribute("value",text);
            node.appendChild(x);
            node.appendChild(textnode);
            document.getElementById("adicionarR").appendChild(node);
            }
        }
</script>
    
asked by Hans Leo Quiroz Marroquin 29.01.2017 в 07:33
source

1 answer

1

Your question, if I have not understood it correctly, is not so much about code but about architecture.

Although I can not understand the reasons for coupling a frontend, so that the persistence of its representation is necessary, I imagine that your project requires it.

My advice, if you want to address your problem in this way, is that if your database allows you to save a JSON field, map your structure to it (JSON) and persist it.

(Postgresql is a good choice)

This way you could recompose your representation of HTML elements and save them dynamically, according to your needs.

This is a partial solution and you will most likely end up feeling short in the future.

Rethink your flow and think if it really is necessary to persist the representation of your frontend or there is another way of design.

Greetings!

    
answered by 29.01.2017 в 11:49