Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

receiving radio box value in php

I have 2 following radio boxes in a form,

<input type="radio" name="radio" value="yes" class="radio" /> Yes
<input type="radio" name="radio" value="no" class="radio" /> No
  1. How can i recieve the value of the radio button once the form is posted (in PHP)
  2. Once it is posted on the same page, how can I remember the selected radio button and keep that checked? Thanks.
like image 642
Jay Avatar asked Mar 02 '11 12:03

Jay


People also ask

How do radio buttons work in PHP?

Use the input with type="radio" to create a radio button. Use the same name for multiple radio buttons to define a radio group. Get the value of a checked radio via the $_POST (or $_GET ) variable, depending on the request method.

How can get submit button value in PHP?

Add type property with submit type="submit" to button tag this will submit the form then receive the form inputs value in php script with super global variable $_POST['input name'] or $_GET[] wdepends on from action property value by default GET.


2 Answers

1) The value of the radio button is saved in $_POST only if any of the choices was selected.

if (isset($_POST['radio']))   // if ANY of the options was checked
  echo $_POST['radio'];    // echo the choice
else
  echo "nothing was selected.";

2) Just check for the value and add checked='checked' if it matches.

<input type="radio" name="radio" value="yes" class="radio" <?php if (isset($_POST['radio']) && $_POST['radio'] == 'yes'): ?>checked='checked'<?php endif; ?> /> Yes
<input type="radio" name="radio" value="no"  class="radio" <?php if (isset($_POST['radio']) && $_POST['radio'] ==  'no'): ?>checked='checked'<?php endif; ?> /> No
like image 117
Czechnology Avatar answered Sep 23 '22 08:09

Czechnology


<input type="radio" name="radio" value="yes" class="radio" /> Yes
<input type="radio" name="radio" value="no" class="radio" /> No

 u get radio value using $_POST['radio'];

simple bro,

<input type="radio" name="radio" <?php if($_POST['radio']=="yes") echo "checked";?> value="yes" class="radio" /> Yes

u have to identify radio box by value man

like image 21
shanmugavel-php Avatar answered Sep 26 '22 08:09

shanmugavel-php