Question

I have a form that has a foreach loop and inside the loop I have an input type radio, so every time it has a new value like the codes below:

<?php
            $aircrafts = AircraftPermit::getaircraft();
            foreach($aircrafts as $aircraft)
                {
                    $pacas = AircraftPermit::getrouteaircraft($aircraft->name ,$pilotid);
                    if($pacas)
                        {
            ?>
                            <tr>
                                <td>
                                    <input type="radio" id="ac" value="" disabled="disabled"><?php echo $aircraft->name ;?><br />
                                </td>
                                <td align="center">
                                    <input name="submit" title="Give permission to selected aircraft" type="submit" value="Give Permit" disabled="disabled">
                                </td>
                                <td align="center">
                                    <font color="green">Granted</font>
                                </td>
                            </tr>
            <?php
                        }
                    else
                        {
            ?>
                            <tr>
                                <td>
                                    <input type="radio" id="ac" value="<?php echo $aircraft->name ;?>"><?php echo $aircraft->name ;?><br />
                                </td>
                                <td align="center">
                                    <input name="id" type="hidden" value="<?php echo $pilotid ;?>">
                                    <input name="submit" title="Give permission to selected aircraft" type="submit" value="Give Permit">
                                </td>
                                <td align="center">
                                    <font color="red">Restricted</font>
                                </td>
                            </tr>
            <?php
                        }
                }
            ?>

Now at the end I have a script to check if the radio button is selected like below:

<script type="text/javascript">
function radio_is_on() {
    var elementId = document.getElementById('ac');
    if (elementId.checked==false)
    {
        alert('Please select the aircraft first!');
        return false;
    }
    if (elementId.value)
    {
        return true;
    }
}
</script>

When the radio button is not pushed the message pops up okay but when it's pushed it returns no value and the form send null value. Please tell me where I'm wrong. Thanks

Was it helpful?

Solution

You cannot have multiple id tags, which is probably why it's not working. Try changing it to a class instead. See below for example in jquery.

HTML:

<input type="radio" class="ac" value="<?php echo $aircraft->name ;?>"><?php echo $aircraft->name ;?><br />

JAVASCRIPT:

function radio_is_on() {
  if ($('.ac:checked').val()) {
    return true;
  }
  else {
    alert('Please select the aircraft first!');
    return false;
  }
}

OTHER TIPS

You have same id for all radio buttons - that is not correct. And only one button is considered all the time. You can number them, eg. ac1, ac2 and so on.

as far as I can remember a radio button is "pushed" when it has the checked attribute, and it is not pushed when it doesn't have it.

...
    if (elementId.hasOwnProperty('checked'))
...

instead of

...
    if (elementId.checked==false)
...
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top