1 Star2 Stars3 Stars4 Stars5 Stars (4 votes, average: 5.00 out of 5)

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 *

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

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

Example of Checked Value in PHP Loop *

[$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>';

}

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 *

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>';

Other examples of Using Checked Value for Radio Inputs *

<label><input type="radio" id="foo" chcked="checked"></label>
<input type="radio" name="foo" value="" checked="checked" />
<input type="radio" name="imgsel" value="" checked>

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

Checked Value Use Form Tag In PHP Loop For Radio Buttons

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.