Assign more than one value to a checkbox

Asked

Viewed 191 times

1

I have a relatively trivial doubt, but I haven’t found the solution anywhere.

When I declare the following tag

 echo '         
                                <tr>
                                <td align="center">'.$skill.'</td>
                                    <td>'.$nome.'</td>
                                    <td>'.$dataconvertida.'</td>
                                    <td>'.$desc.'</td>
                                    <td><input type="checkbox" class="marcar" id="check" value='.$codigo.' name="check[]"/></td>
                                </tr>
                                ';

I am assigning the code value to the checkbox, is it possible to assign more than one value to the checkbox? for example code , name.

  • That one tag is being written by means of a echo ? If yes put the echo also to make it clearer how it opened and how it is closing

  • I added the entire Echo tag

4 answers

1


You can make a multidimensional array at the checkbox.

In the former name:

echo '         
<tr>
    <td align="center">'.$skill.'</td>
    <td>'.$nome.'</td>
    <td>'.$dataconvertida.'</td>
    <td>'.$desc.'</td>
    <td>
        <input type="checkbox" class="marcar" id="check" value='.$codigo.' name="check['.$codigo.']['.$nome.'][]"/>
    </td>
</tr>';

Or in value, with json_encode

<input type="checkbox" class="marcar" id="check" value='.json_encode([$codigo => $nome]).' name="check['.$codigo.']['.$nome.'][]"/> 
  • I can recover this data through a POST? for example if I do $_POST['check[0] -> code $_POST['check[1] -> name

  • Can for $_POST['check'][0]

  • I’ll test it here

  • It worked man, thank you!

0

You can concatenate to get the expected result.

I think it will work

<input type="checkbox" class="marcar" id="check" value='.$codigo.$nome.' name="check[]"
  • using this way he still recognizes only the code, not read the second field

0

You can use the Html5 date attribute to save "extra" date to your checkbox, or enter values delimited by some symbol.

Examples:

function imprimeValores(){
var el = document.getElementById("teste");

console.log(`O valor da checkbox é ${el.value} A data guardada na checkbox é ${el.getAttribute("data-nome")}`);

}


function imprimeValores2(){
var el = document.getElementById("teste2");
el = el.value.split("-");
console.log(`O valor da checkbox é ${el[0]} A data guardada na checkbox é ${el[1]}`);

}
<label for="teste">Clique para ver os valores com data-nome</label><input id="teste" type="checkbox" value="codigo1" data-nome="teste" onclick="imprimeValores()"/>
</br>
<label for="teste2">Clique para ver os valores separados por virgula</label><input id="teste2" type="checkbox" value="codigo2-nome2"  onclick="imprimeValores2()"/>

Now it will depend on how you will use this data.

Reference to date-attributes

0

As yet I can not comment below the comment of gabrielfalieri, i believe that his line of code has a problem in concatenation, which still results in taking only the value of the first field.

<input type="checkbox" class="marcar" id="check" value='.$codigo.$nome.' name="check[]">'.$codigo.$nome;

As an observation of the above code, I do not know if you closed with ">" at the end, if yes, ignore this observation.

I left according to your code not to have to trade much, I believe that only replace the input inside your code for this top, work without problems.

value='.$codigo.$nome.'

I left at the end of the code after ">" to close the input the field values $codigo and $nome to show that the concatenation is working, I left the same concatenation to appear next to the checkbox in the html page the value of $nome and $codigo:

>'.$codigo.$nome;

I believe that now the concatenation of values should work.

Browser other questions tagged

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