Jump to content

Dynamic checkboxes question!


pthurmond

Recommended Posts

Ok, so I have a list of dynamically generated items that is using an array to record each value to be processed by the next script.
I would like to know if this array will only contain the values of the checkboxes that were checked, or will it contain the values of all the generated checkboxes, regardless of whether or not it was checked?
I think it will only contain the values of the checked checkboxes, but I want to be sure.

Here is the code:
[code]
echo '<td width="25"><input name="' . $event[] . '" type="checkbox" value="' . $value . '"></td>';
[/code]

Also I will be processing it as an array in the processing script, will this work?:
[code]
foreach($_POST['event'] as $key => $val)
{
$events[$key] = $val;
}
[/code]


Thanks again for the help,
Patrick
Link to comment
Share on other sites

Thank you for all your help, however the post by toplay confuses me a bit.
First off I am guessing that I don't need to echo event[] outside the string of html.

Secondly are you sure for the value you don't mean this:
[code]
    echo '<td width="25"><input name="event[]" type="checkbox" value="' . $value . '"></td>';
[/code]

Third, can I still use the foreach() loop? And if so, how exactly does it work for a multi-dimensional array?


Thanks,
Patrick
Link to comment
Share on other sites

So would this work just as well?

[code]
foreach($_POST['event'] as $key => $val)
{
$events[$key] = $_POST['event'];
}
[/code]

I am trying to get an array of values. This array will later be converted into a string of comma separated values with the implode function.

Thanks,
Patrick
Link to comment
Share on other sites

[quote author=pthurmond link=topic=114670.msg466646#msg466646 date=1163288644]
Thank you for all your help, however the post by toplay confuses me a bit.
First off I am guessing that I don't need to echo event[] outside the string of html.

Secondly are you sure for the value you don't mean this:
[code]
    echo '<td width="25"><input name="event[]" type="checkbox" value="' . $value . '"></td>';
[/code]

Third, can I still use the foreach() loop? And if so, how exactly does it work for a multi-dimensional array?


Thanks,
Patrick
[/quote]
1) Right (if I understand your question).

2) You can use the period as a concatenation character in the echo, however, the echo is a PHP language construct and you can use commas to seperate values.

3) Yes, you can use foreach(). Example:

[code=php:0]
foreach($_POST['event'] as $key => $val)
{
echo 'Array index: ', $key, ' Value: ', $val;
}
[/code]

So, if you used this: echo '<td width="25"><input name="event[]" type="checkbox" value="1"></td>';

Then it would display:

Array index: 0 Value: 1
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.