darksniperx Posted November 26, 2007 Share Posted November 26, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/79010-solved-checking-array-of-check-boxes/ Share on other sites More sharing options...
phpQuestioner Posted November 27, 2007 Share Posted November 27, 2007 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> Quote Link to comment https://forums.phpfreaks.com/topic/79010-solved-checking-array-of-check-boxes/#findComment-400014 Share on other sites More sharing options...
darksniperx Posted November 28, 2007 Author Share Posted November 28, 2007 thx, I used part of your code to get mine to work. Quote Link to comment https://forums.phpfreaks.com/topic/79010-solved-checking-array-of-check-boxes/#findComment-401149 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.