How to set a default checked custom field in registration form

giorgino

Well-known member
I've set up a custom user field "newsletter" as radio button ("yes" or "not" values) but I need to make "yes" the default choice of the field on registration.

Registrati | immobilio | Forum Immobiliare.webp

How to do this? o_O
 
Ok, so how can I set the default value in registration form *only* but not in user preferences?

Let's just use javascript. It's cleaner:

Admin CP -> Appearance -> Templates -> register_form

Add this code at the top. You need to specify the fieldid and value of the checkbox:

Rich (BB code):
<script type="text/javascript">
$(document).ready(function(){
	$('input[type="checkbox"][name*="[regfield]"][value="[color=red]1[/color]"]').prop('checked',true);
});
</script>
 
Add this code at the top. You need to specify the fieldid and value of the checkbox:
mmm... I've added your piece of code as follow

Code:
<script type="text/javascript">
$(document).ready(function(){
    $('input[type="checkbox"][name*="[newsletters]"][value="1"]').prop('checked',true);
});
</script>

where 'newsletters' is the field_id, but no dice. What's wrong?

Schermata 2014-11-29 alle 15.32.26.webp
 
mmm... I've added your piece of code as follow

Code:
<script type="text/javascript">
$(document).ready(function(){
    $('input[type="checkbox"][name*="[newsletters]"][value="1"]').prop('checked',true);
});
</script>

where 'newsletters' is the field_id, but no dice. What's wrong?

View attachment 90625

To be sure you need to inspect that element. I like to use Chrome. Visit the registration page with Chrome and right click the field and inspect. That will reveal all of the attributes of the field which are being matched with that jQuery selector.

From the screenshot I can tell that it's a radio button and not a checkbox like in my code example. So at the very least you need to specify "radio" instead of "checkbox":

Rich (BB code):
<script type="text/javascript">
$(document).ready(function(){
	$('input[type="radio"][name*="[regfield]"][value="[color=red]1[/color]"]').prop('checked',true);
});
</script>
 
Thanks for the reply @Mike. Can I hack the template as @Jake Bunce mentioned above for a check box default value on registration, then run a query to update everyone else? The default for the dropdown and the radio buttons is in this thread, but not the check box.
 
This java script is for registration page only? I want to change the default check box to ticked in a custom user field. This field is not on my registration page only in the profile page edit.
I too am trying to use a checkbox user field that doesn't apply to the registration page. I was going to use it as an opt-out but that doesn't seem to work for me. The purpose of the checkbox is for an e-mail list. If I make it so if they do check it they are opting out of the e-mail list then when I try to use e-mail users in ACP and leave the user field unchecked in the criteria it still shows the ones that are checked. It would seem a better solution to have a hack that allows me to set the default to checked (opted-in) and then they can uncheck to opt-out. But as I said this won't be part of registration. Is there another hack we can use to check a custom user field by default? Or is there another way I could set the criteria to catch the opted-out option when using ACP to e-mail users? @Jake Bunce
 
That is expected because the template edit only affects the default value in the form. The form must be submitted.

To force an update to existing user records you must run a query:

Rich (BB code):
INSERT INTO xf_user_field_value (user_id, field_id, field_value)
    SELECT user_id, 'field_id', 'value'
    FROM xf_user
ON DUPLICATE KEY UPDATE
    field_value = VALUES(field_value);

You need to specify the field_id of the custom field and the stored value which you want to write to everyone's profile.

Then rebuild the user cache:

Admin CP -> Tools -> Rebuild Caches -> Rebuild User Caches

@Jake Bunce

I apologize for bring up an old thread, but I've searched the forum in-depth to find something to meet these needs. Following the guide in this thread I set up a number of checkboxes that are auto-checked and displayed to users upon registration:

Screen Shot 2017-03-21 at 11.21.25 PM.webp


I wanted to check these boxes for existing users as well and found your above query. I followed your instructions, ran the MySQL query, one for each of the boxes I'd like to autocheck. Then I rebuilt the user cache. I misinformed my query and was hoping for help in removing the useless rows (with some duplicates) I inserted into the database as well as properly inserting the checkbox positive values.

I incorrectly input "true" as the value.

My query looked something like this:

Code:
INSERT INTO xf_user_field_value (user_id, field_id, field_value)


SELECT user_id, ‘email_announcements’, ‘true’

FROM xf_user

ON DUPLICATE KEY UPDATE

field_value = VALUES(field_value);

Correct me if I'm wrong, but to undo my poorly formed query I need to run something like:

Code:
DELETE FROM xf_user_field_value (user_id, field_id, field_value)

SELECT user_id, ‘email_announcements’, ‘true’

FROM xf_user

?

As for inserting positive/true/checked values for the checkboxes, I'm at a loss. When they are checked the field value in MySQL looks like this:

Code:
a:5:{s:16:"email_newsletter";s:16:"email_newsletter";s:11:"email_ezine";s:11:"email_ezine";s:19:"email_announcements";s:19:"email_announcements";s:14:"email_contests";s:14:"email_contests";s:17:"email_promotional";s:17:"email_promotional";}

As far as I can tell a:5: indicates the number of boxes checked. I can't determine why there are duplicates in this location. Does s:16: act as a true or checked value for the value email_newsletter in the field_id newsletter_subscriptions?
 
Last edited:
@Live Free

This will remove all stored values for that field.

Code:
DELETE
FROM xf_user_field_value
WHERE field_id = 'email_announcements'

Then you can run the insert again to set a new default value for existing users.

It looks like a multiple selection field. That stored value is a serialized format with all of the selected key value pairs. You should just copy the stored value for a user that has the desired default selections saved in their profile. Or set only the key value pairs for the boxes you want to be checked by default.
 
Top Bottom