Jump to content


Photo

How do I get multiple checkbox content?


  • Please log in to reply
5 replies to this topic

#1 xprt007

xprt007
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 01 March 2006 - 03:13 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 XenoPhage

XenoPhage
  • Members
  • PipPipPip
  • Advanced Member
  • 99 posts

Posted 01 March 2006 - 03:26 PM

Try this :

   if ($_POST['Eigenschaften']) {
      $varA = implode(' ', $_POST['Eigenschaften'])
   }

You can replace ' ' with whatever delimiter you want. So ',' would give you a comma delimited list.
--
[a href=\"http://blog.godshell.com\" target=\"_blank\"]XenoPhage[/a]
[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]Something mysterious is formed, born in the silent void. Waiting alone and unmoving, it is at once still and yet in constant motion. It is the source of all programs. I do not know its name, so I will call it the Tao of Programming.[/quote]

#3 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 01 March 2006 - 03:41 PM

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:
  • smoker
  • f1fan
  • soccerfan
  • womanizer
First I would modify your form:
<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">
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:
<?php
$qtmp = array();
foreach ($_POST['Eigenschaften'] as $k=>$v) 
   $qtmp[] = $k . "='" . $v . "'";
$q = "update dbtable set " . implode(', ',$qtmp) . "where id='" . $_POST['id'] . "'";
?>
Of course you have to modify the query to suit your purposes.

Ken

#4 xprt007

xprt007
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 01 March 2006 - 04:59 PM

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
$sql = mysql_query("INSERT INTO customers(customerId,fname,sname,Eigenschaften,....) values (...,....,'$varA', .....

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

if($sql)
                        {
                                $customerId = mysql_insert_id();
...

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.

#5 xprt007

xprt007
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 01 March 2006 - 05:57 PM

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

#6 xprt007

xprt007
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 02 March 2006 - 03:40 PM

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) View Post[/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]







0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users