Jump to content

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>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.