bugzy Posted July 16, 2012 Share Posted July 16, 2012 Hello guys! I have this checkbox on my form <td>Choose Category</td> <td> <?php for($i = 0; $cat_num > $i; $i++) { echo "<input type=\"checkbox\" name=\"item_cat[]\" value=". mysql_result($cat_result,$i,'cat_id') . ""; if(isset($_POST['submit']) AND isset($_POST['item_cat'])) { if(mysql_result($cat_result,$i,'cat_id') == $_POST['item_cat']) { echo "checked=\"checked\""; } } echo " />". mysql_result($cat_result,$i,'cat_name') ."<br>"; } ?> </td> Once a user click the submit button, I want those checkboxes that he chose to be checked. My code above is not working because it showing that a user didn't check any even if he did. Anyone? Quote Link to comment https://forums.phpfreaks.com/topic/265793-help-with-getting-back-values-in-checkbox/ Share on other sites More sharing options...
bugzy Posted July 16, 2012 Author Share Posted July 16, 2012 I've added a foreach and it seemed like working already. Here's the new code <td>Choose Category</td> <td> <?php for($i = 0; $cat_num > $i; $i++) { echo "<input type=\"checkbox\" name=\"item_cat[]\" value=". mysql_result($cat_result,$i,'cat_id') . ""; if(isset($_POST['submit']) AND isset($_POST['item_cat'])) { foreach($_POST['item_cat'] as $cat_get) { if(mysql_result($cat_result,$i,'cat_id') == $cat_get) { echo "checked=\"checked\""; } } } echo " />". mysql_result($cat_result,$i,'cat_name') ."<br>"; } ?> </td> Problem is when I print_r it I'm getting this <?php Array ( [0] => 84 [1] => 86 ) checked="checked" />McDo Array ( [0] => 84 [1] => 86 ) />Burgoo Array ( [0] => 84 [1] => 86 ) checked="checked" />Dong Array ( [0] => 84 [1] => 86 ) />Jackson Array ( [0] => 84 [1] => 86 ) />Maxi Dress Array ( [0] => 84 [1] => 86 ) />Supper Array ( [0] => 84 [1] => 86 ) />Shakeys ?> Can you guys check if I have problem with concatenation? Quote Link to comment https://forums.phpfreaks.com/topic/265793-help-with-getting-back-values-in-checkbox/#findComment-1361997 Share on other sites More sharing options...
Barand Posted July 16, 2012 Share Posted July 16, 2012 try if (in_array(mysql_result($cat_result,$i,'cat_id'), $_POST['item_cat'])) { echo "checked=\"checked\""; } Quote Link to comment https://forums.phpfreaks.com/topic/265793-help-with-getting-back-values-in-checkbox/#findComment-1361998 Share on other sites More sharing options...
bugzy Posted July 16, 2012 Author Share Posted July 16, 2012 Solved by myself guys! Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/265793-help-with-getting-back-values-in-checkbox/#findComment-1361999 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.