【问题标题】:How to get the values of the radio buttons with variable names in php?如何在php中获取具有变量名的单选按钮的值?
【发布时间】:2017-06-06 08:08:42
【问题描述】:

此代码从数据库中获取一些数据并用一组单选按钮显示它。如何获取每组单选按钮的值??

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

【问题讨论】:

    标签: php button radio


    【解决方案1】:

    这些点是不需要的。也许你的意思是?:

    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";
    

    假设这是您想要的,您可以获得这样的值(我更喜欢在打印部分(或您的表单,在这种情况下)上方书写):

    if (!empty($_POST)) {
        $tech = $_POST['tech'];
    
        echo 'Value of the second row: '.$tech[1];
    }
    

    【讨论】:

    • 此代码返回一个错误,指出“未定义的偏移量”。无论如何谢谢@ Edvin Tenovimas。任何其他答案表示赞赏。
    • 如果你明白了,那可能是因为你至少没有选择一个。或者比你写的要多。此代码可能存在一些问题。
    猜你喜欢
    • 2015-12-31
    • 1970-01-01
    • 2015-06-15
    • 2018-05-04
    • 1970-01-01
    • 1970-01-01
    • 2022-01-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多