Search code examples
phpjqueryfuelphp

How to disable radio buttons according to the DB query result?


<?php foreach ($types_show as $size): ?>
    <td class='size_rdo'><?php echo Form::radio('radio_size', $value)?></td>
<?php endforeach;?>

This will display some radio buttons,I want to send the $value to the php. if there is the value in DB, the radio button is able, otherwise set it to disable.

I write some Pseudocode below,thanks for answering.

 $(':radio[name=radio_size]').each(function(){
        $.ajax({
            type: "GET",
            url: "/management/order/get_size",
            data: {radio_size:radio_size},
            cache: false,
            dataType: 'json',
            success: function(data) {
                if (data) {
                $(this).attr("disabled",true);
             }
            },
        });
    });

Solution

  • I think that FuelPhp allow you to do it in PHP with adding attributes on the Form helper (I don't know this framework, but it seems to work like another framework I know, see the documentation).

    <?php foreach ($types_show as $size): ?>
        <td class='size_rdo'>
            <?php if($value) // Here I don't understand what you want to test on your 
                             // condition, you must correct it depending of your needs
                 // If the value is not good, then add the disabled attribut
                 echo Form::radio('radio_size', $value, array('disabled'))?>
             else // If the value is the good one, just continue like you did
                 echo Form::radio('radio_size', $value)?>
        </td>
    <?php endforeach;?>
    

    Then you don't need Javascript