Jump to content

Help with getting back values in checkbox


bugzy

Recommended Posts

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?

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?  :shrug:

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.