Jump to content

How do I get multiple checkbox content?


xprt007

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?
Link to comment
Share on other sites

What does your database record look like? Do you want to store each of the values in individual fields or are they put in one field? If they are individual fields, what are the field names?

Here is one solution that assumes that each checkbox updates it's own field and the field names are:[list][*]smoker[*]f1fan[*]soccerfan[*]womanizer[/list]
First I would modify your form:
[code]
<input type="hidden" name="Eigenschaften[smoker]" value="no">
<input type="hidden" name="Eigenschaften[f1fan]" value="no">
<input type="hidden" name="Eigenschaften[soccerfan]" value="no">
<input type="hidden" name="Eigenschaften[womanizer]" value="no">
td nowrap colspan="3"> Smoker:
              <input type="checkbox" name="Eigenschaften[smoker]" value="yes" class="check">
   Formular1 Fan:
              <input type="checkbox" name="Eigenschaften[f1fan]" value="yes" class="check">
   Soccer Fan:
              <input type="checkbox" name="Eigenschaften[soccerfan]" value="yes" class="check">
   Womanizer:
              <input type="checkbox" name="Eigenschaften[womanizer]" value="womanizer" class="check">
[/code]
The hidden fields in the form initialize the Eigenschaften array to all "no". Since only the values of the checkboxes that are actually checked are changed, you will always know what was or wasn't checked.

In your processing script:
[code]<?php
$qtmp = array();
foreach ($_POST['Eigenschaften'] as $k=>$v)
   $qtmp[] = $k . "='" . $v . "'";
$q = "update dbtable set " . implode(', ',$qtmp) . "where id='" . $_POST['id'] . "'";
?>[/code]
Of course you have to modify the query to suit your purposes.

Ken
Link to comment
Share on other sites

Hi XenoPhage & kenrbnsn,
Thank you for the very rapid responses. :)

I first tried XenoPhage's suggestion...
When I I tried to enter $varA together with other variables into database using
[code]$sql = mysql_query("INSERT INTO customers(customerId,fname,sname,Eigenschaften,....) values (...,....,'$varA', .....[/code]

I got "Arrays" again as database entry, whereas I got the 4 checked entries if I tried to echo it without entering it into database.

I then tried kenrbnsn's suggestion, but may be got mixed up while trying to "customize" the code to fit what I've been using, ie making it use "INSERT" on a separate line. Since the customer entry is new & is just being created at that point ... the next line of code actually creates the ID

[code]if($sql)
                        {
                                $customerId = mysql_insert_id();[/code]...

and so what I came up with, left the "Eigenschaften" column empty.
Is it possible to integrate that entry under $sql, or at least to use INSERT and bearing in mind that ID is just being created then?

I am relatively new to PHP as my questions may suggest, so please dont be too brief.
Thanks again to both of you.
Link to comment
Share on other sites

I just noticed that I had made an un-intended double-posting. Apparently in the first posting, at [a href=\"http://www.phpfreaks.com/forums/index.php?showtopic=87173\" target=\"_blank\"]http://www.phpfreaks.com/forums/index.php?showtopic=87173[/a] I got a response that did work. Thanks again
Link to comment
Share on other sites

Can anyone help?

Please refer to that link & the posting below to get my dilemma.
As said in that quote, I got the correct entry into the database of content from the four checked checkboxes.
I discovered later, after my last posting that, strangely, that when I try to get that data from the database as a logged in user (using <? echo $_SESSION['Eigenschaften']; ?>), that particular field is blank, whereas all the other unrelated user information is shown.
Is the "array" background of that data the problem? Anyway, how do I get it also shown in my members profile??

[!--quoteo(post=350704:date=Mar 1 2006, 06:57 PM:name=xprt007)--][div class=\'quotetop\']QUOTE(xprt007 @ Mar 1 2006, 06:57 PM) [snapback]350704[/snapback][/div][div class=\'quotemain\'][!--quotec--]
I just noticed that I had made an un-intended double-posting. Apparently in the first posting, at [a href=\"http://www.phpfreaks.com/forums/index.php?showtopic=87173\" target=\"_blank\"]http://www.phpfreaks.com/forums/index.php?showtopic=87173[/a] I got a response that did work. Thanks again
[/quote]


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.