How to check a radio button

Asked

Viewed 10,495 times

2

My doubt is like checking if a radio button was selected!

I have a while as it is finding values within a table, he keeps adding the radios Buttons. So when the radio is selected and is clicked on "GO" it will call the other pag. passing the radio values. But it always has the option of not having selected anything ... How should I perform this check?

  • 1

    If nothing is selected, the value of the radio will be null.

  • Yes, I know that! But as I include a radio to each value found in the table, it will always be the same name, and the value appears as undefinid!

  • do an if to check if the value is different from Undefined

  • Yes that’s right @Bacco!

1 answer

3


Here is 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 preselected value using checked, but should check the case of being null anyway:

   <input type="radio" name="teste" value="um" checked />Um<br>
   //                                             ^^^
  • This helped a lot @ Bacco. Thanks for sharing!!

  • @Did Felipe test it? If everything is okay, let me know, because then I won’t move anymore. I was trying to test here, but my link is bad (several errors of connection, the cable must have made fun)

  • Yes it helped @Bacco perfectly! The problem I was having in checking whether or not I was selected has been solved!

  • It is not recommended to delete errors $escolha = @$_POST['teste']; do so $escolha = (isset($_POST['teste']) ? $_POST['teste'] : NULL); and if( is_null($escolha ))...

  • 1

    @Carlos I understand that you are wanting to help, but read the comment I put immediately after the deletion, in the same line. The value is already tested in the next line, so your suggestion is redundant. Beware of such "good practices" (the "not recommended" one), because without knowing what you are doing, you will at least write code aimlessly and/or inefficient (so much so that, probably you could not explain the advantage obtained with your suggestion).

  • Okay @Bacco, I didn’t mean your answer didn’t work or anything like that. As Stackoverflow says: Acceptance doesn’t mean it’s the best answer, it just means it was useful to the person who asked. I was just complementing, saying that in good programming practices the error suppression is only for when there is no better alternative. In other words, the use of isset and is_null is much more recommended in the medium than the use of @ in operations. Hugs.

  • 2

    @Carlos I know what you are saying, but I just want to make it clear that the deletion in this case is perfectly valid, because the value will be tested in the next line. Your proposal simply does more testing unnecessarily, with zero benefit. I’m not worried about winning an argument dispute, I just wanted to clarify that "good practices" are useless if used blindly, and you can benefit from understanding when it’s good and when it’s not. However, as I said, I understand that you want to help, and thank you, but this will be read by many people, so I prefer to "demystify".

Show 2 more comments

Browser other questions tagged

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