Problems understanding how to use radio buttons.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
bbo
Forum Newbie
Posts: 4
Joined: Thu Feb 07, 2013 2:08 pm

Problems understanding how to use radio buttons.

Post by bbo »

I'm trying to get the selected radiobutton value of a group of radiobuttons with no luck. I wonder if anyone could tell me what i'm doing wrong here.
I just want to point out that I'm a newbee to PHP coding.

Here is the code:

<div style="position: absolute; left: 9px; top: 412px; width: 364px; height: 85px;" align="left">
<form id="form1" name="form1" method="post" action="product.php">
<input type="radio" name="colsel" id="colsel" value= "Hvid" />
<label id="color_white"> &nbsp; &nbsp; </label>&nbsp; &nbsp;
<input type="radio" name="colsel" id="colsel" value= "Sort" checked="checked" />
<label id="color_black"> &nbsp; &nbsp; </label>&nbsp; &nbsp;
<input type="radio" name="colsel" id="colsel" value= "Grå" />
<label id="color_grey"> &nbsp; &nbsp; </label>&nbsp; &nbsp;
<input type="radio" name="colsel" id="colsel" value="Lys rød" />
<label id="color_lightred"> &nbsp; &nbsp; </label>&nbsp; &nbsp;
<input type="radio" name="colsel" id="colsel" value="Rød" />
<label id="color_red"> &nbsp; &nbsp; </label>&nbsp; &nbsp;
<input type="radio" name="colsel" id="colsel" value="Pink" />
<label id="color_pink"> &nbsp; &nbsp; </label><br />
<input type="radio" name="colsel" id="colsel"value="Blå" />
<label id="color_blue"> &nbsp; &nbsp; </label>&nbsp; &nbsp;
<input type="radio" name="colsel" id="colsel" value="Lys blå" />
<label id="color_lightblue"> &nbsp; &nbsp; </label>&nbsp; &nbsp;
<input type="radio" name="colsel" id="colsel" value="Grøn" />
<label id="color_green"> &nbsp; &nbsp; </label>&nbsp; &nbsp;
<input type="radio" name="colsel" id="colsel" value="Lime" />
<label id="color_lime"> &nbsp; &nbsp; </label>&nbsp; &nbsp;
<input type="radio" name="colsel" id="colsel" value="Gul" />
<label id="color_yellow"> &nbsp; &nbsp; </label>&nbsp; &nbsp;
<input type="radio" name="colsel" id="colsel" value="Orange" />
<label id="color_orange"> &nbsp; &nbsp; </label><br />
<input type="radio" name="colsel" id="colsel" value="Lilla" />
<label id="color_purple"> &nbsp; &nbsp; </label>&nbsp; &nbsp;
<input type="radio" name="colsel" id="colsel" value="Sølv" />
<label id="color_silver"> &nbsp; &nbsp; </label>&nbsp; &nbsp;
<input type="radio" name="colsel" id="colsel" value="Guld" />
<label id="color_gold"> &nbsp; &nbsp; &nbsp; </label>
</form>
</div>
<?php
$_SESSION["color"] = $_POST["colsel"];
// $_SESSION["size"] = 'sizetable';
?>
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Problems understanding how to use radio buttons.

Post by Christopher »

Maybe add a submit button at the bottom of the form:

<input type="submit" name="submit" value="Submit" />
(#10850)
User avatar
mecha_godzilla
Forum Contributor
Posts: 375
Joined: Wed Apr 14, 2010 4:45 pm
Location: UK

Re: Problems understanding how to use radio buttons.

Post by mecha_godzilla »

Hi,

Just to add to Christopher's point, you're not actually doing anything with the radio button value except placing it in a session value that you can't see - for testing purposes you should echo() it out to the browser like this:

Code: Select all

$_SESSION["color"] = $_POST["colsel"];
echo 'Here is the color: ' . $_SESSION["color"];
Also, you don't know which option you're selecting on the form because there are no labels next to the radio buttons. Where you have

Code: Select all

<input type="radio" name="colsel" id="colsel" value= "Hvid" />
<label id="color_white"> &nbsp; &nbsp; </label>&nbsp; &nbsp;
change it to

Code: Select all

<input type="radio" name="colsel" id="colsel" value= "Hvid" />
<label id="color_white"> &nbsp; Hvid &nbsp; </label>&nbsp; &nbsp;
HTH,

Mecha Godzilla
Post Reply