Jump to content

Archived

This topic is now archived and is closed to further replies.

jwwceo

variable withiin $_POST

Recommended Posts

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

Share this post


Link to post
Share on other sites
You should be using...

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

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

[code=php:0]
<?

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>
[/code]

me!

Share this post


Link to post
Share on other sites

×

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.