xprt007 Posted March 1, 2006 Share Posted March 1, 2006 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? Quote Link to comment https://forums.phpfreaks.com/topic/3831-how-do-i-get-multiple-checkbox-content/ Share on other sites More sharing options...
XenoPhage Posted March 1, 2006 Share Posted March 1, 2006 Try this :[code] if ($_POST['Eigenschaften']) { $varA = implode(' ', $_POST['Eigenschaften']) }[/code]You can replace ' ' with whatever delimiter you want. So ',' would give you a comma delimited list. Quote Link to comment https://forums.phpfreaks.com/topic/3831-how-do-i-get-multiple-checkbox-content/#findComment-13301 Share on other sites More sharing options...
kenrbnsn Posted March 1, 2006 Share Posted March 1, 2006 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 Quote Link to comment https://forums.phpfreaks.com/topic/3831-how-do-i-get-multiple-checkbox-content/#findComment-13306 Share on other sites More sharing options...
xprt007 Posted March 1, 2006 Author Share Posted March 1, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/3831-how-do-i-get-multiple-checkbox-content/#findComment-13335 Share on other sites More sharing options...
xprt007 Posted March 1, 2006 Author Share Posted March 1, 2006 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 https://forums.phpfreaks.com/topic/3831-how-do-i-get-multiple-checkbox-content/#findComment-13352 Share on other sites More sharing options...
xprt007 Posted March 2, 2006 Author Share Posted March 2, 2006 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] Quote Link to comment https://forums.phpfreaks.com/topic/3831-how-do-i-get-multiple-checkbox-content/#findComment-13570 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.