Can you show the code that displays the radio buttons?
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?
@devfrey Here you go:
input type="radio" name="gender" value="male" Male
input type="radio" name="gender" value="female" Female
@HIMANSHU_WKD - That's not enough context. Are you using Blade?
<input
type="radio"
name="gender"
value="male" {{ $user->gender === 'male' ? 'checked' : '' }}>
@jeffreyway @devfrey Thanks for your responses.
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' : '' }}>
If solved, are you going to mark it solved.
@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.
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
@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>
@mnrafg because the quotes inside the string get escaped. But why would you ever put this inside blade braces?
<body class="test">
Please or to participate in this conversation.