Bibu
Bibu

Reputation: 201

find what radio button is checked in php

I want to determine which radio button is checked...This is how I populate my list of radio buttons:

$sql="SELECT * from intrebari where cod_chestionar='".$_SESSION['cod']."' ";
$result=mysql_query($sql);

echo "<br><br>";
echo "<table border='1'>";

while($row=mysql_fetch_array($result))

{ 
echo "<tr>";
echo "<td>";
 echo "<input type='radio' name='intrebare' value=''>";
 echo $row[denumire_intrebare];
 echo "<br>";
 echo "</td>";
 echo "</tr>";

 }

echo "</table>";

Now let's say this brings me a list of 4 elements. How do I find out which radio buttons are checked. If is the first one or the second or... .

Upvotes: 0

Views: 1178

Answers (1)

Reinder Wit
Reinder Wit

Reputation: 6615

first you need to assign some sort of unique value to the radiobutton:

   ... 
   while( $row = mysql_fetch_array($result) )
    { 
        echo "<tr><td>";
        echo "<input type='radio' name='intrebare' value='" . $row["someColumnName"] . "'/>";
        echo $row[denumire_intrebare];
        echo "<br/></td></tr>";
    }
    ...

then, when a POST is done, you can retrieve that value by:

    if( isset($_POST["intrebare"]) ) {
        switch ($_POST["intrebare"]) {
            case "value1":
                 // do something
                 break;
            case "value2":
                 // do something else
                 break;
            case "value3:
                 // do something
                 break;
        }
    }

it's important to check if the value exists using isset(), because a radio button does not have to be checked

Upvotes: 3

Related Questions