How to check a radio button

2

My question is how to check if a radio button has been selected!

I have a while as you are finding values within a table , it adds the buttons radios . So when radio is selected and clicked on "IR" it will call the other pag. passing the values of the radius. But you always have the option of not selecting anything ... How should I do this?

    
asked by anonymous 01.09.2014 / 14:11

1 answer

3

Here's a simple example of how to read the Radio Button in PHP.

HTML:

<form action="recebe.php">
   <input type="radio" name="teste" value="um">Um<br>
   <input type="radio" name="teste" value="dois">Dois<br>
   <input type="radio" name="teste" value="três">Três<br>
</form>

PHP:

$escolha = @$_POST['teste']; // Usado @, já que vai ser testado em seguida.

if isempty( $escolha ) $pagina='URL da pagina padrao...';
else if $escolha == 'um' $pagina= 'URL da pagina um';
else if $escolha == 'dois' $pagina= 'URL da pagina dois';
else if $escolha == 'tres' $pagina= 'URL da pagina tres';
else $pagina = 'URL da pagina padrao, ou de erro';

...

Remembering that you can force one of the inputs to have the pre-selected value using checked , but you should check the case for being null anyway:

   <input type="radio" name="teste" value="um" checked />Um<br>
   //                                             ^^^
    
01.09.2014 / 14:30