Dave2136 Posted April 21, 2011 Share Posted April 21, 2011 Hi. My form involves a checkbox. Below is the code. The problem is that when I am not sure what to put on the php. I need for people to be able to check one box tow boxes or all three boxes. On the form side I have this: type="checkbox" name="formcheck[]" value="C". Not sure what to put on the php side and need some assistance <label style="display: block; padding-left: 15px; text-indent: -15px; width:650px;"> <input style="width: 45px; height: 45px; padding: 0; margin:0; vertical-align: bottom; position: relative; top: -1px; *overflow: hidden;" type="checkbox" name="formcheck[]" value="A" /> Directory Listing </label> <label style="display: block; padding-left: 15px; text-indent: -15px;width:650px;"> <input style="width: 45px; height: 45px; padding: 0; margin:0; vertical-align: bottom; position: relative; top: -1px; *overflow: hidden;" type="checkbox" name="formcheck[]" value="B" /> Full page listing </label> <label style="display: block; padding-left: 15px; text-indent: -15px;width:650px;"> <input style="width: 45px; height: 45px; padding: 0; margin:0; vertical-align: bottom; position: relative; top: -1px; *overflow: hidden;" type="checkbox" name="formcheck[]" value="C" /> Receive a link to your website </label> Quote Link to comment https://forums.phpfreaks.com/topic/234396-trouble-getting-checkbox-on-form-uploaded-to-sql/ Share on other sites More sharing options...
requinix Posted April 21, 2011 Share Posted April 21, 2011 $_POST["formcheck"] will be an array of the values that were checked. You can do whatever you want then. What do you want to do with it, anyways? Quote Link to comment https://forums.phpfreaks.com/topic/234396-trouble-getting-checkbox-on-form-uploaded-to-sql/#findComment-1204681 Share on other sites More sharing options...
Dave2136 Posted April 22, 2011 Author Share Posted April 22, 2011 The form asks pick A pick B or pick C, or...pick any combination of the three. Then if they pick one two or three of the choices then I wanted the php to take the choices and to upload that data to mySql. So if they picked A and C to have my SQL have the letters A and C marked and to have B left blank. Quote Link to comment https://forums.phpfreaks.com/topic/234396-trouble-getting-checkbox-on-form-uploaded-to-sql/#findComment-1204734 Share on other sites More sharing options...
DavidAM Posted April 22, 2011 Share Posted April 22, 2011 As requinix said, $_POST["formcheck"] will be an array of the boxes checked. So in your processing code you could do something like this: if (isset($_POST["formcheck"])) { foreach($_POST["formcheck"] as $choice) { // At this point, $choice will be A or B or C // So do what you want with it } } else { // None of the boxes was checked } Quote Link to comment https://forums.phpfreaks.com/topic/234396-trouble-getting-checkbox-on-form-uploaded-to-sql/#findComment-1204745 Share on other sites More sharing options...
philip315 Posted April 22, 2011 Share Posted April 22, 2011 Ok I dont have the code you gave me working yet but looking at it it seems as if taht code would be $choice. Now $choice would be either A, B or C right? What I was looking for was to be able to pick more than one letter such as A and B. I have 3 checkboxes so I took them into php by $formcheck1=$_POST['formcheck']['value']; $formcheck2=$_POST['formcheck']['value']; $formcheck3=$_POST['formcheck']['value']; I wasnt sure whether to place 'value' as 'name' but it doesnt matter because neither works. Dont I have to bring the formcheck[] array into the php page with the above code first before I use the code you gave me, and then the code you gave... isnt that just for either A or B or C and not for A and B (one example)? Quote Link to comment https://forums.phpfreaks.com/topic/234396-trouble-getting-checkbox-on-form-uploaded-to-sql/#findComment-1204894 Share on other sites More sharing options...
DavidAM Posted April 22, 2011 Share Posted April 22, 2011 Your (original) input fields: <input type="checkbox" name="formcheck[]" value="A" /> <input type="checkbox" name="formcheck[]" value="B" /> <input type="checkbox" name="formcheck[]" value="C" /> Will allow zero, one or more boxes to be checked. If one or more are checked, an array is posted to your script. If the user checked "A" and "B", the array would be: $_POST['formcheck'][0] = 'A'; $_POST['formcheck'][1] = 'B' If "B" and "C" were checked it would be: $_POST['formcheck'][0] = 'B'; $_POST['formcheck'][1] = 'C' So, the code I posted, walks through this array looking at the values checked. To make is clearer: $whatDidTheUserCheck = array('A' => false, 'B' => false, 'C' => false); if (isset($_POST["formcheck"])) { foreach($_POST["formcheck"] as $choice) { // At this point, $choice will be A or B or C // So do what you want with it $whatDidTheUserCheck[$choice] = true; } } else { // None of the boxes was checked } // This will give you a DEBUG dump of what the user checked var_dump($whatDidTheUserCheck); Just to be clear, Browsers do NOT post checkboxes that are NOT checked. So, the number of elements in the $_POST['formcheck'] array will be the number of boxes checked. As in my examples above, if the user did NOT check box "C", you will NOT get a field POSTed for "C". That is the reason I have the first IF in there -- if the user does NOT check ANY boxes, the 'formcheck' element of the POST array will NOT even exist. Quote Link to comment https://forums.phpfreaks.com/topic/234396-trouble-getting-checkbox-on-form-uploaded-to-sql/#findComment-1204932 Share on other sites More sharing options...
philip315 Posted April 24, 2011 Share Posted April 24, 2011 What I seem to be having a problem with now is actually getting the data onto mySQL. Originally I had three variables called $formcheck1,$formcheck2, and $formcheck3. When I looked at the code you gave me I saw that the variables where changed to $choice. When I make the variable $choice First of all it only gives one data field not three which might make things harder in the future in terms of Sql searches.... and the other thing is that when I add $choice into the data to be entered into mySQl only the last letter checked gets added to the database. So if someone checks A and C, I will see C on the database. Quote Link to comment https://forums.phpfreaks.com/topic/234396-trouble-getting-checkbox-on-form-uploaded-to-sql/#findComment-1205436 Share on other sites More sharing options...
philip315 Posted April 27, 2011 Share Posted April 27, 2011 Is there anybody out there? Quote Link to comment https://forums.phpfreaks.com/topic/234396-trouble-getting-checkbox-on-form-uploaded-to-sql/#findComment-1206665 Share on other sites More sharing options...
DavidAM Posted April 27, 2011 Share Posted April 27, 2011 That code uses a variable called $choice, but ONLY inside the loop. It is used to hold the value from each checkbox, in turn, for only a moment. We use that value to update the array -- $whatDidTheUserCheck -- with TRUE for those boxes that the user checked. Since you have never provided any comments or code indicating how you want to use these values, we have not provided any suggestions on how to do it. There are many, many, many, different ways to use the value(s). Another way to do it, and I'm sure this is not the best way: $userCheckedA = false; $userCheckedB = false; $userCheckedC = false; if (isset($_POST["formcheck"])) { foreach($_POST["formcheck"] as $choice) { if ($choice == 'A') $userCheckedA = true; if ($choice == 'B') $userCheckedB = true; if ($choice == 'C') $userCheckedC = true; } } else { // None of the boxes was checked } Quote Link to comment https://forums.phpfreaks.com/topic/234396-trouble-getting-checkbox-on-form-uploaded-to-sql/#findComment-1206956 Share on other sites More sharing options...
philip315 Posted May 3, 2011 Share Posted May 3, 2011 Oh my God. So sorry! I was expecting a message in my inbox saying that you wrote. I didnt know that you wrote to me so I did not respond. I have since looked at what you say, but I have worked it out another way. I am not using the array. I cant wrap my head around array. Instead of one variable to equal my three checkboxes $choice, I now have just labeled each checkbox a completely different name, so now it is just like any other input function. Now I can make three separate input fields on MySql for data input. If some one picks A, B , C or all three it doesnt matter because each field has nothing to do with each other field. This is a very decent solution to my query and I think I am happy with it. Thank you for all your help and sorry again for not responding last week. Quote Link to comment https://forums.phpfreaks.com/topic/234396-trouble-getting-checkbox-on-form-uploaded-to-sql/#findComment-1209703 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.