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

Himanshu_wkd's avatar

Getting values from radio button

Guys, I am having one radio button for gender. The values is inserted into the db. Now I want to retrieve that value and the radio button to be checked with the value received.

Instance:

If male is received, I want male to be checked. How can this be done?

0 likes
11 replies
devfrey's avatar

Can you show the code that displays the radio buttons?

Himanshu_wkd's avatar

@devfrey Here you go:

input type="radio" name="gender" value="male" Male

input type="radio" name="gender" value="female" Female

JeffreyWay's avatar
<input 
    type="radio" 
    name="gender" 
    value="male" {{ $user->gender === 'male' ? 'checked' : '' }}>
2 likes
Snapey's avatar

If this is part of an edit form then you might want to use old() so that the choice is remembered if there is a validation failure.

Maybe not so much in this case, but useful to know for other radios

<input 
    type="radio" 
    name="gender" 
    value="male" {{ old('gender',$user->gender) === 'male' ? 'checked' : '' }}>
jlrdw's avatar

If solved, are you going to mark it solved.

Snapey's avatar

@mnrafg. sorry, but thats REALLY BAD advice

you should ALWAYS use {{ }} - especially on input fields

use unescaped blade tags in very rare situations where you are very confident about the source,for example when outputting your own error messages.

Snapey's avatar

@mnrafg

apart from the fact that you appear to have clarified your earlier post with some additional comments, there is NO reason to use raw echos here - there is no HTML being output.

Even in the case of 'attribute="value"' there is no problem using {{ }}

I have built MANY websites and the only time I need to use {!! ...!!) can be counted on a few fingers

To further dam your own argument

{!!...!}} is risky specially when printing the data from user input or from some other untrusted source.

old() data is exactly that - data from an untrusted source - so again, recommending {!! !!} inside an input field is BAD

mnrafg's avatar

@SNAPEY - Thank you for the replies, all you said I'm agree with.

Just test the following code with your instance of Laravel because here at my end it doesn't work.

Question: why the body background is not red? Screenshot

<!-- file: laravel-app/resources/views/test.blade.php -->
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Test</title>
    <style>
        .test {
            background-color: red;
        }
    </style>
</head>
<body {{ 'class="test"' }}>
<!--<body {!! 'class="test"' !!}>-->
</body>
</html>
Snapey's avatar

@mnrafg because the quotes inside the string get escaped. But why would you ever put this inside blade braces?

<body class=&quot;test&quot;>

Please or to participate in this conversation.