The following warnings occurred: | ||||||||||||||||||||||||
Warning [2] Undefined variable $unreadreports - Line: 26 - File: global.php(961) : eval()'d code PHP 8.2.25 (Linux)
|
multiple select box or checkboxes in the registration form - Printable Version +- UserSpice (https://userspice.com/forums) +-- Forum: Miscellaneous (https://userspice.com/forums/forumdisplay.php?fid=28) +--- Forum: Modifications and Hackery (https://userspice.com/forums/forumdisplay.php?fid=29) +--- Thread: multiple select box or checkboxes in the registration form (/showthread.php?tid=345) |
multiple select box or checkboxes in the registration form - LBC - 11-11-2016 Hey, I really need to have a multiple select box or some checkboxes in the registration form. Something that people can select or check so the data gets saved in the database to be displayed in their profile. I have tried to do this myself but I'm not getting anywhere really Can someone please help me with this perhaps? multiple select box or checkboxes in the registration form - mudmin - 11-11-2016 You COULD update the user class to handle the extra inputs, but I'm working on something a little bit more elegant. Give me a little bit on this. multiple select box or checkboxes in the registration form - mudmin - 11-11-2016 Ok. This is not super elegant, but it doesn't require you to update the user class. http://hastebin.com/toyaxagipu.xml Let me walk you through it. 1. This file goes in the usersc folder as join.php. This is good practice and stops our updates from breaking your modifications. Note that I copied the original join form and changed the paths to all the includes. 2. I got rid of the "view" and made the entire join form one big file for simplicity. 3. I added 3 checkboxes at the bottom of the form beginning on line 268. One to show you're over 18(over_18), one to show you love userspice(love_us), and one to show that you are human(am_human). 4. I comment in the form how you could store this info in a different db table, but I'm going to work on the assumption that you are going to want it in the users table, so I added 3 more columns with a type of varchar 5 and called them over_18, love_us, and am_human. Note that I'm also leaving the checkboxes to their default value which means that if a box was checked, it will show up in the database as a string of 'on' and if it was empty, that will be null in the database. If you want to change that, you can do some logic before the array to update the databsae. 5. This is not neat, but it's clear. On line 195, after the user is created, I am going back and running a query to find the user's id that we just inserted. We get that id on line 199. 6. Now I'm creating an array of which field names and which information I want to put into the database. As I commented on in the document, if you are storing this info in a table other than users, you are going to want to include the user id along with the checkbox info, so you know who the info belongs to. I hope this helps. Let me know if you have any questions. multiple select box or checkboxes in the registration form - LBC - 11-11-2016 Thank you for taking a look at this Mudmin. I will have a crack at it soon and let you know my results multiple select box or checkboxes in the registration form - mudmin - 11-11-2016 Sounds great. Just fyi, if you want to make that a binary thing in the db where it is 1 for yes and 0 for no, you could do something like... $over18=Input::get('over_18'); if ($over18 == 'on'){ $over18=1; }else{ $over18=0; } multiple select box or checkboxes in the registration form - LBC - 11-14-2016 Hey Mudmin, So far so good! However....I show these same checkboxes in the user_setting.php page so the user can update the selections. I need them to be checked if they have been turned on in the db and unchecked if they have been turned off in the db. So the user can check or uncheck them accordingly and update the profile. Any way that can be done? multiple select box or checkboxes in the registration form - mudmin - 11-14-2016 Yeah...so there are a few ways to do that with foreach loops and stuff like that, but I would manually update the entire row of checkboxes at every update of the user info. I think it's simpler. I would do something like (assuming you have your db showing 1 for checked and 0 for unchecked) Code: $over_18 = Input::get('over_18'); Code: if ($over_18 === 1) { Code: $over_18 = "1"; Code: } else { Code: $over_18 = "0"; Code: } I'm guessing the other part you want to know how to do is to pre-check the boxes... that would be... Code: <input type='checkbox' name='over_18' id='over_18' value='1' <?php if ($user->data()->over_18 == '1') Code: echo "checked='checked'"; ?>> Are you over 18? Note that the last 2 lines are all technically one line multiple select box or checkboxes in the registration form - LBC - 11-15-2016 Hmmm....I have changed the code to what you suggested in reply #2819...like so: (I removed the array thingy otherwise the whole page went blank) <pre> Code: $emailAddress = Input::get('email'); But it does not seem to be working. What have I done wrong? multiple select box or checkboxes in the registration form - LBC - 11-15-2016 It is not adding anything to the db now. It only adds to the db when I update the user_settings.php and even than it still shows "on" in stead of 1 or 0. I also cannot update the user_settings.php by unchecking the box as it gives me an error saying that over 18, love us and am human are required. I am completely stumped and have no clue what to do multiple select box or checkboxes in the registration form - mudmin - 11-15-2016 Can you paste your entire join.php form on pastebin or hastebin and share the link and I will take a look. |