Using the "check" radio button value in HTML and PHP

So, I have questions regarding radio input type in HTML. As you know, you can put checked

as a value, this will mark it as marked.

History - I am getting 0 or 1 value from my database. Then I check if it is 0 or 1 and then check one of the radio buttons as noted.

My code looks like this:

<?php if($pay_op == 0) { ?>

<input type="radio" value="paypal" id="pay_op" checked>PayPal<br />
<input type="radio" value="other" id="other_op">Other<br/>
<input type="submit" id="pay_op_submit" />

<?php } elseif ($pay_op == 1) { ?>

<input type="radio" value="paypal" id="pay_op">PayPal<br />
<input type="radio" value="other" id="other_op" checked>Other<br/>
<input type="submit" id="pay_op_submit" />
<?php } ?>

      

My problem now, when I try to mark another radio button as marked by clicking it, are both radio buttons checked?

I thought it might have something to do with me, checking if a value is returned from the database 0 or 1 and will hold one of the radio buttons until that value is changed. Now, for my question, does anyone know a solution to this problem, so that whenever someone clicks on something other than the default radio button, it actually checks that one, and not both of them?

Any advice is appreciated! =)

Thank!

+3


source to share


1 answer


The radio buttons work mostly like a named group. The browser only cancels the check for a radio button if it is associated with other radio buttons with a name called the name.



<?php 
    if($pay_op == 0) 
    { ?>
        <input name ="myGroup" type="radio" value="paypal" id="pay_op" checked>PayPal<br />
        <input name ="myGroup" type="radio" value="other" id="other_op">Other<br/>
        <input name ="myGroup" type="submit" id="pay_op_submit" />
    <?php 
    } 
    elseif($pay_op == 1) 
    { ?>
        <input name ="myGroup" type="radio" value="paypal" id="pay_op">PayPal<br />
        <input name ="myGroup" type="radio" value="other" id="other_op" checked>Other<br/>
        <input name ="myGroup" type="submit" id="pay_op_submit" />
    <?php 
    } 
?>

      

+3


source







All Articles