Jump to content


Photo

variable withiin $_POST


  • Please log in to reply
1 reply to this topic

#1 jwwceo

jwwceo
  • Members
  • PipPipPip
  • Advanced Member
  • 212 posts

Posted 29 September 2006 - 01:59 AM

I have a bunch of dynamic form fields which are posted on my php page. The field names are being posted with a variable in them like this

<input type ='checkbox'......name='color{$colorid}'....> so each checkbox has it's own id. I think this is working ok, but when I try to get this value on the next page I am having a hard time retrieving the value.

I am trying this:

if(isset($_POST['color$colorid'])) and if(isset($_POST['color.$colorid'])), but I can't get the right syntax.

what am I doing wrong???

James

#2 printf

printf
  • Staff Alumni
  • Advanced Member
  • 889 posts

Posted 29 September 2006 - 02:17 AM

You should be using...

<input type ='checkbox' name='color[<?=$colorid;?>]' value='some_value' />

It makes it much easier, because PHP will load that into an array! is_set does not allow variable assignments, use ! empty () instead!

<?

if ( ! empty ( $_POST['product_id'] ) )
{
	$pid = intval ( $_POST['product_id'] );

	echo 'Product ID: (' . $pid . ')';

	if ( is_array ( $_POST['colors'][$pid] ) )
	{
		foreach ( $_POST['colors'][$pid] AS $color )
		{
			echo ', ' . $color;
		}
	}
	else
	{
		echo ', no colors selected';
	}
}

// this is just here to show you the example

$product_id = 89042567;

?>


<form action='' method='post'>
pid <input type='text' name='product_id' value='<?=$product_id;?>' /><br />
Red <input type='checkbox' name='colors[<?=$product_id;?>][]' value='red' /> <br />
Blue <input type='checkbox' name='colors[<?=$product_id;?>][]' value='blue' /> <br />
Green <input type='checkbox' name='colors[<?=$product_id;?>][]' value='green' /> <br />
Yellow <input type='checkbox' name='colors[<?=$product_id;?>][]' value='yellow' /> <br />

<input type='submit' value='Select Colors' />

</form>

me!






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users