-1

Validate radio buttons with php using the ID not the NAME:

I have 3 button radios but the name of the radios I bring from the bd (I bring the name), until then I have no problems, the question is that since I do not have a name that I wrote manually I can not pick them up by post (if I print the varibles that arrived by post I receive the value without problems). the case is that if I do it with getElementId () how would that value change to php (I tried it with ajax but it did not work). I would greatly appreciate this help.

index.php

$consulta_tabla_estudiante=mysqli_query($conexion,$profesores);

  while ($registroAsistencia =mysqli_fetch_row($consulta_tabla_estudiante))
  {
?>

<tr>
 
 <td><?php echo $registroAsistencia[0]; ?></td>
 <td><?php echo $registroAsistencia[1]; ?></td>
 <td><?php echo $registroAsistencia[2]; ?></td>
 <td><label><input type='radio' name='<?php echo $registroAsistencia[1]; ?>' id="presente" value='presente'> Presente</label></td>
 <td><label><input type='radio' name='<?php echo $registroAsistencia[1]; ?>' id="ausente" value='ausente'> Ausente</label></td>
  
  
  
  
Jonas
  • 121,568
  • 97
  • 310
  • 388
  • 1
    just reading your title „Validate radio buttons with php using the ID not the NAME“ is enough for me to tell you that php is unaware of the context through wich data was sent to the backend. in other words: your question cannot be solved. – GottZ Jul 06 '19 at 18:18
  • 1
    PHP does not know about the id and you cannot use it. But your `value` is the same as the `id` so you could loop trough `$_POST` and check if the values are present. But ultimately there is most likely something wrong with the design if you need a loop. – Hugo Delsing Jul 06 '19 at 18:24
  • Also indicate what could be done with js but the question would be how to pass that value to php. po ajax not achieve it – alvaro white Jul 06 '19 at 18:31
  • Presumably that echo is shorthand for something like (pseudo code) `echo filter_var(trim($rA[0]), FILTER_SANITIZE_SPECIAL_CHARS));` too? I'm not sure it's safe to assume that all data in the db arrived there from our good input code. – pbhj Jul 06 '19 at 18:56

2 Answers2

0

Only one radio button value (at most) for a group of buttons with the same name will be sent with the form. You can access that button's value using the name (not ID) of the button. The id is not sent with the form at all.

Ideally, don't make the name something dynamic. Instead, give it a stable name you can use to get the value.

In the worst case, you can include hidden elements that tell you the names of any radio button groups you have like this, e.g.:

<td><?php echo $registroAsistencia[0]; ?></td>
<td><?php echo $registroAsistencia[1]; ?></td>
<td><?php echo $registroAsistencia[2]; ?></td>
<td><input type='hidden' name='group' value='<?php echo $registroAsistencia[1]; ?>'><label><input type='radio' name='<?php echo $registroAsistencia[1]; ?>' id="presente" value='presente'> Presente</label></td>
<td><label><input type='radio' name='<?php echo $registroAsistencia[1]; ?>' id="ausente" value='ausente'> Ausente</label></td>

Then you can get the value of the group field, which tells you the name of the radio button value to get.

If you have several of these, you can repeat the hidden field and use the name group[] so you get an array of the group names on the PHP side.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
0

in the post what I get is this /

Training
(
[bSend] = & gt; // the button
[diana] = & gt; I presented
[luis] = & gt; absent
[nose] = & gt; excuse
[juan] = & gt; excuse
)

What I need is to know how to collect this data