Test if an input of the type radiobutton is selected in PHP

4

I created a table that is the summary of all my previous calculations and in this table I created a field where I placed a radial input to be able to select one of the calculated options. I need to identify which of the inputs was clicked, using the id, to proceed with the calculations. I'm using the PHP language.

Table:

            <table class="table table-bordered">
            <caption> Calculation </caption>
            <thead>
                <tr>
                        <th class="text-center">Cam number</th>
                        <th class="text-center">Space between cam</th>
                        <th class="text-center">FOV</th>
                        <th class="text-center">Overlap</th>
                        <th class="text-center">Height</th>
                        <th class="text-center">Pixels</th>
                        <th class="text-center">Selected</th>
                </tr>
            </thead>
            <tbody>
                <tr>
                        <td class="text-center"><?php echo"$ncam" ?></td>
                        <td class="text-center"><?php echo"$dist mm" ?></td>
                        <td class="text-center"><?php echo"$fov mm" ?></td>
                        <td class="text-center"><?php echo"$ov mm" ?></td>
                        <td class="text-center"><?php echo"$alt mm" ?></td>
                        <td class="text-center"><?php echo"$sensor" ?></td>
                        <td class="text-center"><input type="radio" name="row-3" id="l1" data-col="1"></td>

                 </tr>
                <tr>
                        <td class="text-center"><?php echo"$ncam1" ?></td>
                        <td class="text-center"><?php echo"$dist1 mm" ?></td>
                        <td class="text-center"><?php echo"$fov1 mm" ?></td>
                        <td class="text-center"><?php echo"$ov1 mm" ?></td>
                        <td class="text-center"><?php echo"$alt1 mm" ?></td>
                        <td class="text-center"><?php echo"$sensor" ?></td>
                        <td class="text-center"><input type="radio" id="l2" ></td>
                 </tr>
             </tbody>
        </table>

Then the idea was to select one or the other option and the calculations to be "automatic."

Thank you.

I made the change here: Script test using radio imput

Here the script works, but putting it in my document stops working, I do not understand why.

At this point I'm trying to pass the radial input variable to a variable in PHP and I'm having an error. At this point I do not know where this error might come from, as I'm passing the variable correctly:

$escolha = $_POST['radio'];

I do not know if the problem is due to having two forms on the same page, I just have to solve this problem to finish. Thanks

    
asked by anonymous 17.11.2015 / 16:48

2 answers

5

To retrieve the value of this input it is necessary to place this table inside a form tag.

<form action="processa-tabela.php" method="post">
    <table>
        <tr>
            <td>...</td>
            <td><input type="radio" name="escolha" value="l"></td>
        </tr>
        <tr>
            <td>...</td>
            <td><input type="radio" name="escolha" value="2"></td>
        </tr>
    </table>
    <input type="submit">Enviar</input>
</form>

In the file processa-tabela.php you will receive the variable like this:

$idEscolha = $_POST['escolha'];

As you have not explicitly added an input radio indicating as selected.

<input type="radio" name="escolha" value="l" selected>

If the user does not select, the variable will not exist, to avoid this problem, check if the variable exists and treat it when it does not exist, or simply add the first line always as selected.

To check if the variable exists, do:

if (isset($_POST['escolha']) === false) {
    // Executa o tratamento de quando não existir
    // pode ser uma mensagem informando que é obrigatório
    // selecionar uma opção.
} else {
    // Executa o calculo quando existir
}
    
17.11.2015 / 17:03
3

First, let's consider the following rule:

  

Radio Button : To select only one value (the same name is maintained for all inputs, what changes is the value. : Used for multiple selections. The name and value are variables.

For the purpose you need, use checkbox. Even if you have to select only one item, you can dial / unmark, while the radio does not support this feature by default.

Now to select only one of the options with checkbox, you can do this:

HTML

<input id="chkCamp1" type="checkbox" data-check="1" value="elemento 1"> elemento 1
<input id="chkCamp2" type="checkbox" data-check="2" value="elemento 2"> elemento 2
<input type="text" data-view>

SCRIPT

$(function() {
    $('[data-check]').on('click', function() {
        var myInput = {valor: $(this).val(), id: $(this).data('check')};
        $('[data-check]').each(function(index, value) {
            ($(this).data('check') == myInput.id) ?
             $(this).attr('checked', true) :
             $(this).attr('checked', false);
             $('[data-view]').val(myInput.valor);
        });
    });
});

Check out the chechbox selection here

To select using radio, it is not necessary, script to limit the amount of selection, but by default it is not disabled, this can be done using script:

HTML

<table class="table table-bordered">
            <caption> Calculation </caption>
            <thead>
                <tr>
                        <th class="text-center">Cam number</th>
                        <th class="text-center">Space between cam</th>
                        <th class="text-center">FOV</th>
                        <th class="text-center">Overlap</th>
                        <th class="text-center">Height</th>
                        <th class="text-center">Pixels</th>
                        <th class="text-center">Selected</th>
                </tr>
            </thead>
            <tbody>
                <tr>
                        <td class="text-center">000</td>
                        <td class="text-center">000 mm</td>
                        <td class="text-center">000 mm</td>
                        <td class="text-center">000 mm</td>
                        <td class="text-center">000 mm</td>
                        <td class="text-center">000 px</td>
                        <td class="text-center"><input type="radio" name="row-3" id="l1" class="rdo" value="1"></td>

                 </tr>
                 <tr>
                        <td class="text-center">000</td>
                        <td class="text-center">000 mm</td>
                        <td class="text-center">000 mm</td>
                        <td class="text-center">000 mm</td>
                        <td class="text-center">000 mm</td>
                        <td class="text-center">000 px</td>
                        <td class="text-center"><input type="radio" name="row-3" id="l2" value="2"  class="rdo" ></td>
                 </tr>
             </tbody>
        </table>
                <label>Valor:</label>
               <input type="text" id="saida">

SCRIPT

$(function() {
    $('.rdo').on('click', function() {
       $('#saida').val( $(this).val() );
    });
});

Check here the selection with radion button

    
17.11.2015 / 17:37