Checked Value Use Form Tag In PHP Loop For Radio Buttons

Recently I was working on a project that required the “checked” value on a radio button. I had put the form item in a php loop to update products for a marketing site. I used the following code.

Example of Checked Value For Radio Select

[code]<label class="btn btn-info active"><input type="radio" name="optradio" id="optradio’" value="shipped" checked>Shipped</label>[/code]

This is suppose to automatically check that value. However I put it in a loop like this.

Example of Checked Value in PHP Loop

[php][$ct=2;
while ($row = $result->fetch_assoc()) {

echo ‘<label class="btn btn-info"><input type="radio" name="optradio" id="optradio’.$ct.’" value="processing" >Processing</label>
<label class="btn btn-info active"><input type="radio" name="optradio" id="optradio’.$ct.’" value="shipped" checked>Shipped</label>’;

}[/php]

The Problem Of Checked Value In A PHP Loop

I was using AJAX to process the forms. So I did not need a form tag, right? Wrong! When you are wanting to auto select a checked value on radio input fields you must use the form tag.

Example Checked Radio Input Value in PHP loop

[php]echo ‘<label class="btn btn-info"><input type="radio" name="optradio" id="optradio’.$ct.’" value="processing" >Processing</label>
<label class="btn btn-info active"><input type="radio" name="optradio" id="optradio’.$ct.’" value="shipped" checked>Shipped</label>’;[/php]

Other examples of Using Checked Value for Radio Inputs

[code]<label><input type="radio" id="foo" chcked="checked"></label>[/code]

[code]<input type="radio" name="foo" value="" checked="checked" />[/code]

[code]<input type="radio" name="imgsel" value="" checked>[/code]

References

Conclusion

So there you have it. Use a form tag when you are in a loop for radio input fields. Then you will be able to auto select your radio input.

Checked Value Use Form Tag In PHP Loop For Radio Buttons was last modified: June 15th, 2017 by Maximus Mccullough

Summary
Checked Value Use Form Tag In PHP Loop For Radio Buttons
Article Name
Checked Value Use Form Tag In PHP Loop For Radio Buttons
Description
Why you need a form tag even with using AJAX for check radio inputs. This is done in a PHP loop.
Author
Publisher
A1WEBSITEPRO LLC
Logo
Checked Value Use Form Tag In PHP Loop For Radio Buttons

Leave a Reply

Your email address will not be published.