Jump to content

Archived

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

xprt007

How do I get multiple checkbox content?

Recommended Posts

Hi,
I need some help urgently.

[code]<td nowrap colspan="3"> Smoker:
              <input type="checkbox" name="Eigenschaften[]" value="smoker" class="check">
   Formular1 Fan:
              <input type="checkbox" name="Eigenschaften[]" value="f1fan" class="check">
   Soccer Fan:
              <input type="checkbox" name="Eigenschaften[]" value="soccerfan" class="check">
   Womanizer:
              <input type="checkbox" name="Eigenschaften[]" value="womanizer" class="check">[/code]

I want to get that input into a database. The problem is when I do it the normal way, ie

$Eigenschaften = $_POST['Eigenschaften'] before entering it into database, I get the entry "array" in database. I must say I added the [] beside the variable name, after I was told arrays need to be used if more than one entry is automatically selected, even if one ticked more than one checkbox. That means I was always getting one entry into database.

After endless googling, I have established one has got to use some form of arrays. There so many different suggestions, but none has led me to the desired result.

An example:

[code] if ($_POST['Eigenschaften'])
        {
        foreach($_POST['Eigenschaften'] as $element)
                {
           echo "$element";
        }
    }[/code]

That echos the entries I want. [!--coloro:#FF0000--][span style=\"color:#FF0000\"][!--/coloro--][Result after checking all the 4 boxes ==> smokerf1fansoccerfanwomanizer] [!--colorc--][/span][!--/colorc--] The problem is as soon as u replace say echo = "$element"; with say
$varA = "$element"; to try & create a variable out of it, .. if one tries to enter it into database or echo it, it only returns one entry from the checkbox.

The many examples I have seen after googling dozens of sites, only "echo" the result. I dont want that. I want all the checked entries together possibly stored in one variable like $varA ... that I can use for different things, before entering them in database.

Does any one have an idea how to get the problem solved?

Share this post


Link to post
Share on other sites
Try this..

if ($_POST['Eigenschaften'])
{
foreach($_POST['Eigenschaften'] as $element)
{
$varA = $varA."$element ";
}
}

Else you could use

$varA=join (', ', $_POST['Eigenschaften']);


Which would do the same thing but proberly the better way..

Share this post


Link to post
Share on other sites
Thanks a great deal. In spite of making endless searches (googling on many PHP related sites) I'd not (yet) come across something like that. May be I have not been looking in the right places.

Share this post


Link to post
Share on other sites
Guest
This topic is now closed to further replies.

×

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.