Picking array values from a form with PHP

Asked

Viewed 73 times

0

I have the following form:

inserir a descrição da imagem aqui

I need to get the values marked with PHP, but I’m not getting it. See below:

....
$c = 1;
$visualizar = '<table class="table table-hover">';
while($peMaterias = mysqli_fetch_object($sqlMaterias)){
    $visualizar .= "<tr>";
    $visualizar .= "<td><i class=\"fas fa-caret-right\"></i> <span style='font-weight: bold'>Usuário ".$c."</span></td>";
    $visualizar .= "<td><div class='radio-group'>
      <label class='radio-label'>
          <input name='Licenca[".$c."]' type='checkbox' value='1'>
          <span class='inner-label' style='color: #008000; font-weight: bold'>Fuma</span>
      </label>";
      $visualizar .= "<td><label class='radio-label'>
          <input name='Licenca[".$c."]' type='checkbox' value='1' >
          <span class='inner-label' style='color: #F00; font-weight: bold'>Bebe</span>
      </label>";
      $visualizar .= "<td><label class='radio-label'>
      <input name='Licenca[".$c."]' type='checkbox' value='1'>
      <span class='inner-label' style='color: #008000; font-weight: bold'>Trabalha</span>
  </label>";
  $visualizar .= "<td><label class='radio-label'>
      <input name='Licenca[".$c."]' type='checkbox' value='1'  >
      <span class='inner-label' style='color: #F00; font-weight: bold'>PcD</span>
  </label>
</div></td></tr>";
$c++;
}

And I’m trying that way:

if($_POST["Submit"] == "Salvar"){
   for($i = 0; $i <= count($_POST["Licenca"]); $i++)
  {
    echo $_POST["Licenca"][$i]."<br>";
  }
}

But you’re only returning me:

1
1

I tried that way too:

 for($i = 0; $i <= count($_POST["Licenca"]); $i++)
  {
    $verdadeira[$i] = ($_POST["Licenca"][$i] == 1)?"S":"N";
    echo $verdadeira[$i]."<br>";
  }

But it returns like this:

N
S
S
  • 1

    You’ve already debugged the code to know exactly what the array is $_POST? Put it in the question, please.

1 answer

1


The problem is that you are generating the array in the wrong way than you intend.

Here..

Licenca[".$c."]

.. in all the checkbox, no matter which will be chosen, will always be the same!

What you can do is this:

Licenca[".$c."][]

To insert another dimension with the values you will use. Or even then:

Licenca[".$c."][fuma]

Entering the key you want to redeem that contains the value:

Your code would look like this:

$visualizar .= "<td><div class='radio-group'>
      <label class='radio-label'>
          <input name='Licenca[".$c."][fuma]' type='checkbox' value='1'>
          <span class='inner-label' style='color: #008000; font-weight: bold'>Fuma</span>
      </label>";
      $visualizar .= "<td><label class='radio-label'>
          <input name='Licenca[".$c."][bebe]' type='checkbox' value='1' >
          <span class='inner-label' style='color: #F00; font-weight: bold'>Bebe</span>
      </label>";
      $visualizar .= "<td><label class='radio-label'>
      <input name='Licenca[".$c."][trabalha]' type='checkbox' value='1'>
      <span class='inner-label' style='color: #008000; font-weight: bold'>Trabalha</span>
  </label>";
  $visualizar .= "<td><label class='radio-label'>
      <input name='Licenca[".$c."][PcD]' type='checkbox' value='1'  >
      <span class='inner-label' style='color: #F00; font-weight: bold'>PcD</span>
  </label>

Then you can rescue them like this:

    for($i = 1; $i <= count($_POST["Licenca"]); $i++)
      {
        foreach($_POST["Licenca"][$i] as $key => $value){
            echo $key . " - " . $value . " | ";
        }
        echo "<br>";
      }
  • 1

    You’re right Andrei. I didn’t pay attention to that detail. It worked! Thank you very much!

  • 1

    @Fox.11 Quiet ... Hug! =)

Browser other questions tagged

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