Jump to content

[SOLVED] checking array of check boxes??


darksniperx

Recommended Posts

what I need to do is while I am retrieving data from database, is to check correct boxes.

 

the following code is of the checkboxes that are displayed on my webpage

<label><input type='checkbox' name='cat_id[]' value='1'/>Food</label><label><input type='checkbox' name='cat_id[]' value='2'/>Gas</label>

 

<php
...
while($category_row = mysql_fetch_array($category_table))
{
?>
category_id = '<?php echo $category_row['cat_id'];?>';
<script type='text/javascript'>
document.GetElementById['cat_id'].checked = 'true';
</script>
<?php
}

 

what I need to do is to fix js script, so that it would take the value from category_id,  find check box with the same value and set checked to true

Link to comment
https://forums.phpfreaks.com/topic/79010-solved-checking-array-of-check-boxes/
Share on other sites

try this:

 

<?php
...
while($category_row = mysql_fetch_array($category_table))
{
?>
<script type='text/javascript'>
var category_id = "<?php echo $category_row['cat_id'];?>";
var phpvar1="1"; // set this variable to what you choose
var phpvar2="2"; // set this variable to what you choose
function checker()
{
if (category_id == phpvar1)
{
  document.getElementById('cat_id1').checked = true;
  document.getElementById('cat_id2').checked = false;
}
else if (category_id == phpvar2)
{
  document.getElementById('cat_id2').checked = true;
  document.getElementById('cat_id1').checked = false;
}
else
{
  document.getElementById('cat_id1').checked = false;
  document.getElementById('cat_id2').checked = false;
}
}
</script>

</head><body onload="checker()">


<label><input type='checkbox' id='cat_id1' value='1'/>Food</label><label><input type='checkbox' id='cat_id2' value='2'/>Gas</label>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.