0
I have an array in PHP that are the values selected from the checkbox, which were obtained by $_GET:
$_GET['selecionados'] = array(2) {
[0]=> string(1) "a"
[1]=> string(1) "b"
}
There I have the chekboxes:
foreach($selecoes = $check){
echo '<input type="checkbox" value="'.$check->valor.'" name="selecionados[]" id="'.$check->valor.'">
}
he prints:
<input type="checkbox" value="a" name="selecionados[]" id="a">
<input type="checkbox" value="b" name="selecionados[]" id="b">
<input type="checkbox" value="c" name="selecionados[]" id="c">
<input type="checkbox" value="d" name="selecionados[]" id="d">
I need that if the value of selected is in $_GET['selected'], it is marked "checked". in this example would be:
<input type="checkbox" value="a" name="selecionados[]" id="a" checked>
<input type="checkbox" value="b" name="selecionados[]" id="b" checked>
<input type="checkbox" value="c" name="selecionados[]" id="c">
<input type="checkbox" value="d" name="selecionados[]" id="d">
How to do it? I imagine using str_replace, but I don’t know exactly how.
pq wrote wrong...rs, values are c and d, I am changing the question...
– Leandro Marzullo
You have already described the logic: if you are in
$_GET['selecionados']
addschecked
. How you tried to implement this?– Woss
the ones I tried didn’t work. I tried to use echo str_replace('id="'. $_GET['selected']. '"', 'id="'. $_GET['selected']. '" checked', $option) ( the $option declared as '<input type="checkbox" value="'. $check->value. '" name="selected[]" id="'. $check->value.'">'
– Leandro Marzullo
Put in question the trials and results of each
– Woss