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?

Link to comment
Share on other sites

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:

Link to comment
Share on other sites

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.