Radio input array in PHP form

Asked

Viewed 96 times

0

I have the following code:

for ($n = 0; $n < $totquestoes; $n++) {
    $al = $n + 1;
    $cod = $linha['codprova'];
    $q[] = $linha['questao'];
    $r1[] = $linha['descquestao1'];
    $r2[] = $linha['descquestao2'];
    $r3[] = $linha['descquestao3'];
    $r4[] = $linha['descquestao4'];

    echo '<label class = "control-label">' . $al . 'º) ' . $q[$n] . '</label></br>
        <input type="radio" name="codresp" value="1"> <label class = "control-label">' . $r1[$n] . '</label></br>
        <input type="radio" name="codresp" value="2"> <label class = "control-label">' . $r2[$n] . '</label></br>
        <input type="radio" name="codresp" value="3"> <label class = "control-label">' . $r3[$n] . '</label></br>
        <input type="radio" name="codresp" value="4"> <label class = "control-label">' . $r4[$n] . '</label></br>                          
        ';
}

He creates the questions and answers with 4 alternatives in radio input, Only I need to change the name of each foor loop of each alternative to be able to validate the questions and answers in the validation form.

How can I change the radio name so I can receive via POST and validate?

  • Do you have any information that is unique to each question, that relates the same to the answers? If you have just concatenate the input names with this value.

  • I have the $Cod, has the question and answer, and how do I receive in the POST?

  • 1

    Bro, the simplest, create a for, name it like input1, input2, etc... so it’ll be easier for you to work. Then put everything inside a Json or even array, inside PHP you can turn into an object and you can work with it quietly with a foreach. This is good because it will even be able to put more inputs in the future without having to worry about messing around with the code a lot. In your case, use codresp1, codresp2, etc....

2 answers

0


Just use the name's in array form concatenating the variable $n of the loop as contents:

name="codresp['.$n.']"

So you will have separate collections of name's for each response group:

<label class = "control-label">1º) </label></br>
<input type="radio" name="codresp[0]" value="1"> <label class = "control-label"></label></br>
<input type="radio" name="codresp[0]" value="2"> <label class = "control-label"></label></br>
<input type="radio" name="codresp[0]" value="3"> <label class = "control-label"></label></br>
<input type="radio" name="codresp[0]" value="4"> <label class = "control-label"></label></br>                          

<label class = "control-label">2º) </label></br>
<input type="radio" name="codresp[1]" value="1"> <label class = "control-label"></label></br>
<input type="radio" name="codresp[1]" value="2"> <label class = "control-label"></label></br>
<input type="radio" name="codresp[1]" value="3"> <label class = "control-label"></label></br>
<input type="radio" name="codresp[1]" value="4"> <label class = "control-label"></label>

By submitting the form, you will receive with $_POST['codresp'] the values selected in array form:

Array
(
    [0] => 1 // 1ª resposta
    [1] => 4 // 2ª resposta
    [2] => 3 // 3ª resposta
)

Where the index [0] is the selected value of the first question, the [1] of the second and so on.

  • 1

    It worked perfectly @Sam vlw.

0

Sam’s answer is good and solves your problem, but based on your comment I will suggest a second alternative:

for ($n = 0; $n < $totquestoes; $n++) {
    $al = $n + 1;
    $cod = $linha['codprova'];
    $q[] = $linha['questao'];
    $r1[] = $linha['descquestao1'];
    $r2[] = $linha['descquestao2'];
    $r3[] = $linha['descquestao3'];
    $r4[] = $linha['descquestao4'];

    echo '<label class = "control-label">' . $al . 'º) ' . $q[$n] . '</label></br>
        <input type="radio" name="{$cod}" value="1"> <label class = "control-label">' . $r1[$n] . '</label></br>
        <input type="radio" name="{$cod}" value="2"> <label class = "control-label">' . $r2[$n] . '</label></br>
        <input type="radio" name="{$cod}" value="3"> <label class = "control-label">' . $r3[$n] . '</label></br>
        <input type="radio" name="{$cod}" value="4"> <label class = "control-label">' . $r4[$n] . '</label></br>                          
        ';
}

I have the $Cod, have the question and answer, and how do I receive in the POST? use

$_POST['cod']

Browser other questions tagged

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