Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

david19's avatar

Radio Button uncheck (best way?)

Hello Team, I use Laravel 6 with Bootstrap: I saw a new problem. I have Categories with Parent Categories. If i check a radio button, then i can not uncheck in my form.

Yes, i can make a standart category_base, and make a if statement and set parent_id to "NULL". But the problem is, in the future i loaded allways this category_base in my "foreach" blade files. I think that is not the best solution.

How you deal with this? Do you make this with javascript, all what is the best way? Many thanks.

0 likes
10 replies
Sinnbeck's avatar

I would suggest instead make an extra option that is 'off' or 'none'. Or use checkboxes

1 like
Tray2's avatar

Radio buttons are use when there is a limited amount of values that can be grouped and are required like < 18 or > 18. For optional values a checkbox should always be used.

1 like
david19's avatar

I found a very go way for me. I think its the best. Put this over the @foreach in the radio form:

<input type="radio" class="radio" name="parent_id" value="0" checked> No Parent Category

In the controller:

if($request->parent_id == 0)
      {
        $category->parent_id = NULL;
      }

Thats it! :D

Tray2's avatar

Yes that would work but it's not a good practice to use a hammer to remove screws from a wall. It sure works but it's not the correct tool for the trade.

1 like
david19's avatar

@tray2 Yes, the checkbox are better, but checkbox allows multiple selects. Can you show me a example with <18 or >18 please?

Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

And if you need to do that this is slot cleaner

$category->parent_id = $request->parent_id ?: null;
Nakov's avatar

@tray2 and why isn't it? That's the off state that @sinnbeck suggested. I would go with the same, because he either needs 1 parent ID or no parent ID (from a list of parent categories so it is not just black or white). Checkboxes in this case won't work because he has more than 1 parent categories.

1 like
Tray2's avatar

@nakov The question asked about unchecking a radiobutton and the only way to do that is by clicking another one in that group. If there were only one radiobutton it would be much better to have a checkbox. If there are multiple values and only one should be used I would not use checkboxes (Maybe a select though).

1 like
Nakov's avatar

@tray2 Yeah, a <select> is a better alternative to a radio buttons if there are many parent categories. Select with default "No parent" category option would do the same.

1 like
Tray2's avatar

@david19

Something like

<input type="radio" name="gender" value="male"> Male<br>
<input type="radio" name="gender" value="female"> Female<br>
1 like

Please or to participate in this conversation.