Testing whether a radiobutton input is selected in PHP

Asked

Viewed 1,951 times

4

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

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 were "automatic".

Thank you.


I made the change here :Script test using radio imput

Here the script works, but if you put it in my document it stops working, I don’t understand why.


Right now I’m trying to move the radial input variable to a PHP variable and I’m having an error. Right now I don’t know where this error might come from, because I’m passing the variable correctly:

$escolha = $_POST['radio'];

I don’t know if the problem is due to having two Forms on the same page, I just have to solve this problem to finish. Thank you

  • input uses the same name for all radio’s so only one can be selected and after Submit is sent the value of that selected radio

  • No one has any idea how I can do it?

2 answers

5


To recover the value of this input you need 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 archive processa-tabela.php you will receive the variable like this:

$idEscolha = $_POST['escolha'];

Since you did not explicitly add a radio input 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 do the treatment for when it does not exist, or simply add the first row always as selected.

To check if the success is done:

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
}
  • First of all, thank you, instead of going to another page, I intend to stay the same. To do this I remove the action parameter and then to receive the variable type: if ($_SERVER['REQUEST_METHOD']== "POST") {$idEscolha = $_POST['choice'];}

  • Quiet ;) But even if sending to the same page, it is important to keep the action, as it is a mandatory attribute for the tag form in HTML. Working on the same file is already your choice.

  • So the action parameter is Empty? Something like action="""? My idea in using radial imput is to use it as "Submit", ie select one or another option. Thanks for the help.

  • No, put the name or URL of the same file. Leaving links, form, etc without a defined destination can generate some problems with some versions of browsers.

  • Hello, I have a little problem. I’m trying to put it this way. But although it works, I always get a message that the variable does not exist. And when sending refreshes everything asking again the data, as they were already entered is full of error messages. How can I get around this situation? Thank you.

  • I didn’t quite understand your dubious @Pedrolima, which varivelta giving as non-existent? If it has already been inserted once the guy should open the same page by editing the existing data? If you just insert recommend putting the processing in another file, and at the end of the process puts header('Location: arquivo.php'); so if the guy updates the form will not be processing forever or keep appearing the message of what data were sent.

Show 1 more comment

3

First, let us consider the following rule::

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

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

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

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 here the chechbox selection

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

  • First of all thank you, I can understand the message. If I use check boxes I always have the opportunity to have no option selected again. It’s an interesting aspect and I hadn’t thought about it. I’m thinking of using one of these two approaches to perform Submit instead of selecting and then pressing Submit. That was the idea, I would press directly on the desired line and the program would continue,.

  • I’m trying to apply your code to my program, I’ve already added the class as well as value. Then I added the Script to the body, but I can’t get the same result.

  • @Pedrolima, don’t forget to include the jquery library.

  • I put the library but it wasn’t working. I had to download it and put it in the application. Now I just need to know how to send my choice without having to load Submit. Just click on one or the other, the program send the table data and make the rest of the calculations.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.