-2

i've need to submit all these $_POST values in this foreach loop, but i can't find a way to put this working, i'm sending in a foreach loop because the name of all the fields are like this = name=name of the field[1].

foreach($_POST['pergunta'] as $pergunta, $_POST['tipo'] as $tipoPergunta, $_POST['valor'] as $valor, $_POST['correta'] as $correta)

What is the correct way to make this foreach loop work since i need to submit all of these values?

This is the form code:

<form action="paginas/professor/cadastraAtividade.php" method="post" id="FormAtividades">
  <table class="table table-responsive" id="questoes">
    <tbody>
      <tr>
        <td>
          <select id="opt1" name="tipo[1]" onchange="checkQuest(this)" class="browser-default">
            <option selected="selected" disabled="disabled">Selecione um tipo</option>
            <option value="Aberta">Aberta</option>
            <option value="Multipla">Múltipla Escolha</option>
            <option value="CertoErrado">Certo ou Errado</option>
            <option value="Justifica">A primeira justifica a Segunda</option>
          </select>
          <div id="perg1"><label id="lblP[1]" for="pergunta[1]">Insira a Pergunta</label><input name="pergunta[1]" id="pergA[1]" class="materialize-textarea" style="height: 57px;"></div>
          <div id="pergCont1"></div> <label for="valor[1]" class="active">Insira o valor desta Atividade</label> <input type="text" id="valor1" name="valor[1]"></td>
        <td><button type="button" name="addPerg" id="addPerg" onclick="addQuestao()">Adicionar Pergunta</button></td>
      </tr>
      <tr id="coluna2">
        <td>
          <select id="opt2" name="tipo[2]" onchange="checkQuest(opt2)" class="browser-default">
            <option selected="selected" disabled="disabled">Selecione um tipo</option>
            <option value="Aberta">Aberta</option>
            <option value="Multipla">Múltipla Escolha</option>
            <option value="CertoErrado">Certo ou Errado</option>
            <option value="Justifica">A primeira justifica a Segunda</option>
          </select>
          <div id="perg2"></div>
          <div id="pergCont2"></div> <label>Insira o valor desta Atividade</label> <input type="text" id="valor2" name="valor[2]"></td>
        <td><button type="button" name="remPerg" class="btn_remove" id="2">X</button></td>
      </tr>
      <tr id="coluna3">
        <td>
          <select id="opt3" name="tipo[3]" onchange="checkQuest(opt3)" class="browser-default">
            <option selected="selected" disabled="disabled">Selecione um tipo</option>
            <option value="Aberta">Aberta</option>
            <option value="Multipla">Múltipla Escolha</option>
            <option value="CertoErrado">Certo ou Errado</option>
            <option value="Justifica">A primeira justifica a Segunda</option>
          </select>
          <div id="perg3"><label for="pergunta">Insira a Pergunta</label><input name="pergunta[3]" id="perg[3]" class="materialize-textarea"></div>
          <div id="pergCont3"><label id="lblAlt1M" for="alt1[3]">Alternativa A</label><input type="text" id="alt1M" name="alt1[3]"> <label id="lblAlt2M" for="alt2[3]">Alternativa B</label><input type="text" id="alt2M" name="alt2[3]"> <label id="lblAlt3M" for="alt3[3]">Alternativa C</label>
            <input
              type="text" id="alt3M" name="alt3[3]"> <label id="lblAlt4M" for="alt4[3]">Alternativa D</label><input type="text" id="alt4M" name="alt4[3]"> <label id="lblAlt5M" for="alt5[3]">Alternativa E</label><input type="text" id="alt5M" name="alt5[3]"> <label id="CM" for="correta[3]">Alternativa Correta</label>
              <select
                class="browser-default" id="corrM" name="correta[3]">
                <option value="A">A</option>
                <option value="B">B</option>
                <option value="C">C</option>
                <option value="D">D</option>
                <option value="E">E</option>
                </select>
          </div> <label>Insira o valor desta Atividade</label> <input type="text" id="valor3" name="valor[3]"></td>
        <td><button type="button" name="remPerg" class="btn_remove" id="3">X</button></td>
      </tr>
      <tr id="coluna4">
        <td>
          <select id="opt4" name="tipo[4]" onchange="checkQuest(opt4)" class="browser-default">
            <option selected="selected" disabled="disabled">Selecione um tipo</option>
            <option value="Aberta">Aberta</option>
            <option value="Multipla">Múltipla Escolha</option>
            <option value="CertoErrado">Certo ou Errado</option>
            <option value="Justifica">A primeira justifica a Segunda</option>
          </select>
          <div id="perg4"><label id="lblPCE" for="pergunta[4]">Insira a Pergunta</label><input name="pergunta[4]" id="perg[4]" class="materialize-textarea"></div>
          <div id="pergCont4"><label id="lblCCE" for="correta[4]">Alternativa Correta</label>
            <select class="browser-default" name="correta[4]" id="corr[4]">
              <option value="Certo">Certo</option>
              <option value="Errado">Errado</option>
            </select>
          </div> <label>Insira o valor desta Atividade</label> <input type="text" id="valor4" name="valor[4]"></td>
        <td><button type="button" name="remPerg" class="btn_remove" id="4">X</button></td>
      </tr>
      <tr id="coluna5">
        <td>
          <select id="opt5" name="tipo[5]" onchange="checkQuest(opt5)" class="browser-default">
            <option selected="selected" disabled="disabled">Selecione um tipo</option>
            <option value="Aberta">Aberta</option>
            <option value="Multipla">Múltipla Escolha</option>
            <option value="CertoErrado">Certo ou Errado</option>
            <option value="Justifica">A primeira justifica a Segunda</option>
          </select>
          <div id="perg5"><label id="lblPerg1" for="perg1[5]">Insira a Primeira Pergunta</label><input type="text" id="perg1J" name="perg1[5]"> <label id="lblPerg2" for="perg2[5]">Insira a Segunda Pergunta</label><input type="text" id="perg2J" name="perg2[5]"></div>
          <div id="pergCont5"><label id="lblCorrJ" for="correta[5]">Alternativa Correta</label>
            <select class="browser-default" id="corrJ" name="correta[5]">
              <option value="A">As duas afirmações são verdadeiras, e a segunda justifica a primeira.</option>
              <option value="B">As duas afirmações são verdadeiras, e a segunda não justifica a primeira.</option>
              <option value="C">A primeira afirmação é verdadeira, e a segunda é falsa.</option>
              <option value="D">A primeira afirmação é falsa, e a segunda é verdadeira.</option>
              <option value="E">As duas afirmações são falsas.</option>
            </select>
          </div> <label>Insira o valor desta Atividade</label> <input type="text" id="valor5" name="valor[5]"></td>
        <td><button type="button" name="remPerg" class="btn_remove" id="5">X</button></td>
      </tr>
    </tbody>
  </table>
  <i class="btn waves-effect waves-light gradient-45deg-light-blue-indigo waves-input-wrapper" style=""><input class="waves-button-input" type="submit" value="Enviar"></i>
</form>
MTCoster
  • 5,868
  • 3
  • 28
  • 49
veloxSZ
  • 23
  • 6
  • 1
    what does this have to do with mysql? You posted no code for it. We also need to see the form and sample inputs. – Funk Forty Niner Nov 18 '18 at 21:09
  • that's not valid php, perhaps: `foreach ($_POST as $post) {echo $post['pergunta'];}` but im not really understand what you are doing here –  Nov 18 '18 at 21:10
  • @IdontDownVote i'm needing to send each value of these 4 $_POST methods to my database. – veloxSZ Nov 18 '18 at 21:14
  • you've an answer below. Again please; relevance to mysql is? (Never did get a response for it and being the first comment) You also have some JS happening, but no database code or RDBMS used. – Funk Forty Niner Nov 18 '18 at 21:40
  • @FunkFortyNiner ghostbin.com/paste/kwqtu i've just not posted the entire code. My mysql functions rely on this db.class.php file. – veloxSZ Nov 18 '18 at 22:23

1 Answers1

2

You can't just snap a bunch of arrays to foreach. You have to do do something like:

for ($i = 0; $i < count($_POST['pergunta']); ++$i)
{
    $pergunta     = $_POST['pergunta'][$i];
    $tipoPergunta = $_POST['tipo'][$i];
    $valor        = $_POST['valor'][$i];
    $correta      = $_POST['correta'][$i];
    // do stuff
}

Or if you absolutely want to use foreach:

foreach ($_POST['pergunta'] as $i => $pergunta)
{
    $tipoPergunta = $_POST['tipo'][$i];
    $valor        = $_POST['valor'][$i];
    $correta      = $_POST['correta'][$i];
    // do stuff
}

Also, doesn't it look a bit silly to you that it's the client that is telling you what the correct answer is? This information should be kept secret.

Havenard
  • 27,022
  • 5
  • 36
  • 62
  • the foreach code works like a charm, but i'm getting one more problem, when i submit my form i have 4 types of questions: aberta,certoerrado,multipla and justifica. The types: aberta,certoerrado,multipla just submit fine to the db, but justifica doesn't send nothing and it works like on mulitpla type. Here is the entire page code: https://ghostbin.com/paste/kwqtu. About the correct question value: on my project i have 2 levels: student and teacher, and only the teacher can submit it. The student will see this only after it submit his answers. – veloxSZ Nov 18 '18 at 22:17
  • @Gianvelox I can't tell what's wrong with this code alone except that it screams [SQL Injection attack](https://en.wikipedia.org/wiki/SQL_injection) all over it. Rule number one of database: All input is evil until proven otherwise. Related question: https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php?rq=1 – Havenard Nov 18 '18 at 22:46