Sanjana Kubendran
Sanjana Kubendran

Reputation: 11

How to get the values of the radio buttons with variable names in php?

This code gets some data from the database and displays it with a group of radio buttons. How do I get the value for every group of radio buttons??

<form name="functional"  method="post" action="funcsub.php">

    <?php
    $n=0;
    $con=mysqli_connect('localhost','Sanjana','sanjana');
    mysqli_select_db($con,'mydatabase');

    $sql = mysqli_query($con,"select * from functional") or die("Failed");
    while($result = mysqli_fetch_array($sql)){
    ?>
        <br/>
        <?php
        echo $result["Skill"];
        echo "</br>";
        echo"<input type='radio' name='tech[.$n.]' value='0'>0";
        echo"<input type='radio' name='tech[.$n.]' value='1'>1";         
        echo"<input type='radio' name='tech[.$n.]' value='2'>2";         
        echo"<input type='radio' name='tech[.$n.]' value='3'>3";          
        echo"<input type='radio' name='tech[.$n.]' value='4'>4";
        echo"<input type='radio' name='tech[.$n.]' value='5'>5";
        $n=$n+1;
    }  
    ?>   
    <br/>
    <input type="submit" name="submit" value="Submit">
</form>    

Upvotes: 1

Views: 1129

Answers (1)

Gynteniuxas
Gynteniuxas

Reputation: 7093

Those dots are not needed in these. Perhaps you meant?:

echo "<input type='radio' name='tech[".$n."]' value='0'>0";
echo "<input type='radio' name='tech[".$n."]' value='1'>1";
echo "<input type='radio' name='tech[".$n."]' value='2'>2";
echo "<input type='radio' name='tech[".$n."]' value='3'>3";
echo "<input type='radio' name='tech[".$n."]' value='4'>4";
echo "<input type='radio' name='tech[".$n."]' value='5'>5";

Assuming that's what you wanted, you can get values like this (I prefer writing above the printing part (or your form, in this case)):

if (!empty($_POST)) {
    $tech = $_POST['tech'];

    echo 'Value of the second row: '.$tech[1];
}

Upvotes: 1

Related Questions