Saving dynamically created inputs

0

Hello, I would like to ask for help with this project, I put it in github to make it easier. link

Next, there are multiple-choice questions that are created dynamically, and there are the answers that are created dynamically. I want to save the answers with the id of the question. I already mounted the sql and it is working, my question is syntax. I send the questions and answers through ajax. I get the post, and I call the insert function.

   function Gravar() {
        try {
             $.ajax({
                type: "post",
                url: "RecebendoDados.php",
                dataType: "text",
                async: true,

                data: {
                    quest: document.getElementById("quest").value,
                    descr: document.getElementById("descr").value,
                    categ: document.getElementById("categ").value,
                    autor: document.getElementById("autor").value,
                    perguntasAbertas: $(".perguntasA").serializeArray(),
                    perguntasFechadas: $(".perguntasM").serializeArray(),
                    respostas2:  $('.respostasP').serializeArray()

                },
                success: function (resp) {
                    if (resp === "Não") {
                        $("#exampleModa2").modal("show");
                    }
                    if (resp === "SIM") {
                        $("#exampleModal").modal("show");
                    } else {
                        alert("Não foi possivel realizar está operação porque foram encontrados erros. ");
                        alert(resp);
                    }
                    //alert(resp[0]);
                },
                error: function () {
                    alert('ERRO DO SISTEMA Nº22.\nENTRE EM CONTATO COM O ADMINISTRADOR DO SISTEMA.');
                }
            });
        } catch (exception) {
            console.log(exception);
        }

    }

My PHP

$this->con->beginTransaction();
        $stmt = "INSERT INTO
      questionario (myid, nome_questionario, descricao, categoria, data, data_update, autor)
      VALUES (NULL, :nome_questionario, :descricao, :categoria, :data, :data2, :autor);";
        $stmt = $this->con->prepare($stmt);
        $stmt->bindValue(":nome_questionario", $nome_questionario);
        $stmt->bindValue(":descricao", $descricao);
        $stmt->bindValue(":categoria", $categoria);
        $stmt->bindValue(":autor", $autor);
        $stmt->bindValue(":data", $datas);
        $stmt->bindValue(":data2", $data2);

        $stmt->execute();
        $id_Questionario = $this->con->lastInsertId();


        foreach ($perFechadas as $perguntaF) {
            $sql = "INSERT INTO perguntas (myid, id_questionario, texto_pergunta) values (NULL, :id_questionario, :texto_pergunta);";
            $sql = $this->con->prepare($sql);
            $sql->bindValue(":id_questionario", $id_Questionario);
            $sql->bindValue(":texto_pergunta", $perguntaF);
            $sql->execute();
            $idPerguntF = $this->con->lastInsertId();
            for ($cont = 0; $cont < $qntPerguntas; $cont++) {
                foreach ($respostas2 as $RespostasValor) {
                    $sql_2 = "INSERT INTO respostas (myid, texto_resposta, id_perguntas) values (NULL, :texto_resposta, :id_perguntas);";
                    $sql_2 = $this->con->prepare($sql_2);
                    $sql_2->bindValue(":texto_resposta", $RespostasValor);
                    $sql_2->bindValue(":id_perguntas", $idPerguntF);
                    $sql_2->execute();
                }
            }

         foreach ($perAbertas as $perguntasAbertas) {
            $sql ="INSERT INTO perguntas (myid, id_questionario, texto_pergunta) values (NULL, :id_questionario, :texto_pergunta);";
            $sql = $this->con->prepare($sql);
            $sql->bindValue(":id_questionario", $id_Questionario);
            $sql->bindValue(":texto_pergunta", $perguntasAbertas);
            $sql->execute();

        $this->con->commit();
    
asked by anonymous 28.03.2018 / 19:32

0 answers