0

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... .

j0k
  • 22,600
  • 28
  • 79
  • 90
Bibu
  • 201
  • 3
  • 10
  • 25
  • 3
    $_POST['intrebare'] holds the value of the checked one... if you submit an form... – Mathlight Sep 18 '12 at 06:26
  • You will however have to assign different `value` attributes to each radio button. There's plenty of tutorials and howtos on that on the web, by the way – codeling Sep 18 '12 at 06:28
  • yes..but how do I know which one is it... – Bibu Sep 18 '12 at 06:28
  • 1
    possible duplicate of [which radio button is checked?](http://stackoverflow.com/questions/7086846/which-radio-button-is-checked) – codeling Sep 18 '12 at 06:29
  • @Bibu: did you read *my whole comment*? assign a value to each button, and $_POST['intrebare'] will contain the value of the selected one – codeling Sep 18 '12 at 06:30
  • @Bibu the value of the checked one is passed with POST, so the one's that aren't selected, aren't posted.... – Mathlight Sep 18 '12 at 06:30
  • they are different approaches..so leave it alone.. – Bibu Sep 18 '12 at 06:31
  • And if you want to do it client side: http://stackoverflow.com/questions/596351/how-can-i-get-which-radio-is-selected-via-jquery – Mathlight Sep 18 '12 at 06:31
  • what is a different approach? the question I linked to? it certainly deals with exactly the same topic – codeling Sep 18 '12 at 06:32
  • Not directly related, but useful nonetheless: Don't use `mysql_*`, it's begun the deprecation process. Use `mysqli_*` or `PDO` instead – Elias Van Ootegem Sep 18 '12 at 06:56

1 Answers1

3

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

Reinder Wit
  • 6,490
  • 1
  • 25
  • 36
  • and if I want to refer to that value that is checked..let's say: for radio button 1 checked do that...how can I do that? – Bibu Sep 18 '12 at 06:52
  • @Bibu `if($_POST['intrebare'] == "THE_VALUE_OF_RADIO_1"){ // do stuf } elseif($_POST['intrebare'] == "THE_VALUE_OF_RADIO_2"){ // do stuff}` – Mathlight Sep 18 '12 at 06:55
  • @Bibu Well, look at the swtich statement: http://php.net/manual/en/control-structures.switch.php and do the switch on $_POST['intrebare'] – Mathlight Sep 18 '12 at 06:58
  • there must be another approach..because the values are created and the values will differ from 30 to 100... – Bibu Sep 18 '12 at 07:00
  • what needs to be done based on the values? It's impossible to create logic based on values that are not set yet – Reinder Wit Sep 18 '12 at 07:04