user3496349
user3496349

Reputation: 189

Retrieving the Value of a Radio Button using PHP

Code for RadioButton:

<h1 style="margin:0; margin-top:10px; padding:0; padding-left:25px; padding-bottom:10px; font-family:sans-serif;">
</h1>
<div style="background:#1794FF; color:#fafafa; padding:10px;">
<h3></h3>
<table>
<tr>
<td>
<input type="radio" name="radiog_lite" id="radio1" class="css-checkbox" />
<label for="radio1" class="css-label">Neighbourhood Only</label>
</td>
<td>
<input type="radio" name="radiog_lite" id="radio2" class="css-checkbox" checked="checked"/><label for="radio2" class="css-label">Zipcode Only</label>
</td>
<td>
<input type="radio" name="radiog_lite" id="radio3" class="css-checkbox" />
<label for="radio3" class="css-label">Near Zipcode</label>
</td>
<td>
<input type="radio" name="radiog_lite" id="radio4" class="css-checkbox" />
<label for="radio1" class="css-label">City-Wide</label>
</td>
</tr>
</table>
</div>
<div style="background:#1794FF; color:#222; padding:10px;">

Php Code:

 <?PHP

 $selected_radio = $_POST['radiog_lite'];
  print $selected_radio;

   ?>

After a form submission I arrive at the code above. However, it says the value is "on". Why isn't it printing the chosen radio button name?

Upvotes: 0

Views: 196

Answers (4)

Neeraj Kumar
Neeraj Kumar

Reputation: 504

Use form method "post" and also put value for each radio:

<form method="post">
<table>
<tr>
<td>
<input type="radio" name="radiog_lite" id="radio1" class="css-checkbox" value="Neighbourhood Only" />
<label for="radio1" class="css-label">Neighbourhood Only</label>
</td>
<tr>
</table>
<input type="submit" />
</form>

Upvotes: 0

user3505541
user3505541

Reputation: 25

May you can assign a value:

PHP Code

 <form method="POST">
    <input type="radio" name="radiog_lite" id="radio1" class="css-checkbox" value="1" />
    <label for="radio1" class="css-label">Neighbourhood Only</label><br>
    <input type="radio" name="radiog_lite" id="radio2" class="css-checkbox" checked="checked" value="2"/>
    <label for="radio2" class="css-label">Zipcode Only</label><br>
    <input type="radio" name="radiog_lite" id="radio3" class="css-checkbox" value="3" />
    <label for="radio3" class="css-label">Near Zipcode</label><br>
    <input type="radio" name="radiog_lite" id="radio4" class="css-checkbox" value="4" />
    <label for="radio1" class="css-label">City-Wide</label><br>
    <input type="submit" value="Submit">
 </form>

PHP Code:

 <?php
  $selected_radio = $_POST['radiog_lite'];
  print $selected_radio;
 ?>

Upvotes: 1

J&#233;r&#244;me
J&#233;r&#244;me

Reputation: 292

Use a value attribute on your input radio.

Upvotes: 0

bingorabbit
bingorabbit

Reputation: 695

A radio button is has a default value of "on", you should specify your value in your input initialization like so:

<input type="radio" name="radiog_lite" id="radio2" class="css-checkbox" value="Your Value Here" checked="checked"/><label for="radio2" class="css-label">Zipcode Only</label>

So you can replace "Your Value Here" with "Zipcode Only" for this one. The label is just to describe the radio button value for the front-end user.

Upvotes: 0

Related Questions