Jump to content

variable withiin $_POST


jwwceo

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
Link to comment
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!

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.