Jump to content


Photo

How do I get multiple checkbox content?


  • This topic is locked This topic is locked
2 replies to this topic

#1 xprt007

xprt007
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 01 March 2006 - 03:51 PM

Hi,
I need some help urgently.

<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">

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:

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

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?

#2 shocker-z

shocker-z
  • Members
  • PipPipPip
  • Advanced Member
  • 864 posts
  • LocationNottingham

Posted 01 March 2006 - 04:01 PM

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..
www: www.ukchat.ws | irc: irc.ukchat.ws chan: #blufudge

#3 xprt007

xprt007
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 01 March 2006 - 05:54 PM

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.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users