Question

I have radio buttons for Yes and No and want to pre-populate them from a value in a mysql database. I am able to show the radio buttons but they always show the Yes checked even when the returned value is No. I am also not able to change the pre-populated value eg Yes to No.

<input  type="radio" id="etag" name="etag" value="Yes" <?php echo ($d_etag == 'Yes') ? 'checked="checked"' : ''; ?> />Yes
<input  type="radio" id="etag" name="etag" value="No" <?php echo ($d_etag == 'No') ? 'checked="checked"' : ''; ?> />No
Was it helpful?

Solution

Note : id = should b unique as mentioned by Phil in his comments

This is how you can achieve ur task!

<input  type="radio" id="etag_1" name="etag" value="Yes" <?php echo ($d_etag == 'Yes') ? 'checked="checked"' : ''; ?> />Yes
<input  type="radio" id="etag_2" name="etag" value="No" <?php echo ($d_etag == 'No') ? 'checked="checked"' : ''; ?> />No

To find out Which Radio button is clicked use

$("input:radio[name=etag]").click(function() {
    var value = $(this).val();
});

or try this

$('input:radio[name=etag]:checked').val();

OTHER TIPS

Can you var_dump($d_etag); and check the data type of that variable. it should be a string with Case-sensitive.

Can you try this:

<input  type="radio" id="etagYes" name="etag" value="Yes" <?php echo ($d_etag == 'Yes') ? 'checked="checked"' : ''; ?> />Yes
<input  type="radio" id="etagNo" name="etag" value="No" <?php echo ($d_etag == 'Yes') ? '' : 'checked="checked"'; ?> />No
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top