is there a way to group radio button for php usage?

Dora picture Dora · Feb 25, 2013 · Viewed 8.9k times · Source

I'm wondering if let's say I have 4 radio buttons named red blue yellow and green but if none of them are unchecked and a person submits the form, let's say will echo no radio checked. if I use the if statement I can only think of using something like

if(empty($_GET(["red"])) || empty($_GET(["blue"])) || empty($_GET(["green"])) ||empty($_GET(["yellow"])))
{
echo "blah";
}

But that's really long for that if statement if there's two more radio I want to add or something let's say I have a name field an ya....that's gonna be a long long if statement...

is there a way to group them together and make them shorter?

Answer

sjdaws picture sjdaws · Feb 25, 2013

Radio buttons are grouped by default if you give them the same name

<input type="radio" name="group" value="1"> Option 1  
<input type="radio" name="group" value="2"> Option 2

The selected option will then be passed to php in $_POST['group']