The following warnings occurred:
Warning [2] Undefined variable $unreadreports - Line: 26 - File: global.php(961) : eval()'d code PHP 8.2.25 (Linux)
File Line Function
/global.php(961) : eval()'d code 26 errorHandler->error
/global.php 961 eval
/showthread.php 28 require_once





× This forum is read only. As of July 23, 2019, the UserSpice forums have been closed. To receive support, please join our Discord by clicking here. Thank you!

  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
multiple select box or checkboxes in the registration form
#1
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 Sad

Can someone please help me with this perhaps?
  Reply
#2
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.
  Reply
#3
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.
  Reply
#4
Thank you for taking a look at this Mudmin. I will have a crack at it soon and let you know my results Smile
  Reply
#5
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;
}
  Reply
#6
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?
  Reply
#7
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:
}
Or something like this. This way, you are checking the value of over_18 every time they are updating.

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
  Reply
#8
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');
//This is clunky, but gets back your user id of the new user
$findNewUser = $db->query("SELECT * FROM users WHERE email = ?",array($emailAddress));
$newUser = $findNewUser->first();
$newId = $newUser->id;

//now you have to decide where you want those checkboxes to go.
//the left side is the column in the db, the right side is what you are inputting

$over18=Input::get(‘over_18’);
if ($over18 == ‘on’){
$over18=1;
}else{
$over18=0;
}

$loveus=Input::get(‘love_us’);
if ($loveus == ‘on’){
$loveus=1;
}else{
$loveus=0;
}

$amhuman=Input::get(‘am_human’);
if ($amhuman == ‘on’){
$amhuman=1;
}else{
$amhuman=0;
}

//If you want this info to exist in the existing user table, you do
$db->update('users',$newId,$moreInfo);

//If you want it to be a new row in the db, let's say more_info
//note that you would also want to pass in 'id'=>$newId into the array
//above so you know whose data this is!
//$db->insert('more_info',$moreInfo);

Redirect::to($us_url_root.'users/joinThankYou.php');
}

} //Validation and agreement checbox
} //Input exists

?>
</pre>


But it does not seem to be working. What have I done wrong?
  Reply
#9
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 Sad
  Reply
#10
Can you paste your entire join.php form on pastebin or hastebin and share the link and I will take a look.
  Reply


Forum Jump:


Users browsing this thread: 4 Guest(s)