Keep checkbox values

Hi there.

I have a mysql db with a table cars (id, name), equipment (id, desc) and car_equip (id, id_car, id_equip). For each car, user can select several extras, by checking the chosen checkboxes. Later, the user might want to change some data.
I want, when clicking on some edit button, the extras popup to bring all the extras chosen before, already checked.

I supose I can use something like this:

" />

where $_POST[“extra_value”] comes for a db query. I will select the extras from car_equip for a particular car.

My problem is, the extras list are too long. How can I do this, with a loop for example? I don’t want to change code whenever some extra is added or deleted.

Sorry the long post. I don’t figure how can I explain this better…

Thanks in advance. Regards,


please post the code u already have!

u may use arrays:
[php]$resource=mysql_query(‘SELECT * FROM equipment’);

//and then to make the checkboxes:
foreach($equipment as $id => $desc)
echo ‘<input name=“extra[]” type=“checkbox” value="’.$id.’" id=“extra’.$id.’” ‘;
if(in_array($_POST[‘extra’],$id)) echo ‘checked=“checked”’;

//and for the sql
$sql = ‘SELECT * FROM car_equip WHERE equip_id IN (’.implode(’,’,$_POST[‘extra’]).’)’;[/php]

hope this helps

Hi agains.

Your code helped…

I used something like this…

$myarray=explode(", ",$_SESSION["equip"]); 
//extras comes from db, separated by commas
echo "<input class="normal" type="checkbox" name="list1" value="".$row['descricao']." (".$row['preco']." EUR)" ";
echo (in_array($row['descricao']." (".$row['preco']." EUR)",$myarray)) ? "checked="checked"" : "";
echo "/></td>n";

Well… it may be not the best way, but it works… :)

Thanks again. Regards,


Sponsor our Newsletter | Privacy Policy | Terms of Service