Jay
Jay

Reputation: 11179

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.

Upvotes: 15

Views: 81454

Answers (4)

Czechnology
Czechnology

Reputation: 15012

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

Upvotes: 24

xkeshav
xkeshav

Reputation: 54072

1) u will receive only that radio box value via POST which is checked

    $radio_value=$_POST['radio'];

2)

<input type="radio" name="radio" value="yes" class="radio" 
   <?php echo ($radio_value == 'yes') ? 'checked="checked"' : ''; ?>/> Yes
<input type="radio" name="radio" value="no" class="radio" 
   <?php echo ($radio_value == 'no') ? 'checked="checked"' : ''; ?>/> No

Upvotes: 2

shanmugavel-php
shanmugavel-php

Reputation: 410

<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

Upvotes: 5

Quentin
Quentin

Reputation: 944455

How can i recieve the value of the radio button once the form is posted (in PHP)

$_POST['radio']

Once it is posted on the same page, how can I remember the selected radio button and keep that checked?

Add a checked attribute if the value is equal to $_POST['radio'].

Upvotes: 2

Related Questions