【问题标题】:php- dynamically created radio buttons handlephp-动态创建的单选按钮句柄
【发布时间】:2013-04-15 04:04:25
【问题描述】:

所以.. 我有一个上传页面,我可以上传一个充满数据的 xml 表。我在页面本身上处理它(在页面关闭后不将其发送到任何数据库) 我不知道每次会上传多少行,但它不会大于 100。

现在,我得到了这部分,作为一个可以上传所有数据的表单,用户需要在为每一行创建的单选按钮中选择一个值(单选按钮的值:1-5):

<form enctype="multipart/form-data" name="myForm" method="post" action="<?php echo $_SERVER['PHP_SELF']?>" onsubmit="return checkForm()">
    <?php $numRow=0;?>
    <?php //echo "<input type='radio' name='difficulty".$numRow."' value='11'>11";?>
    <table border="1">
        <tr> 
            <th>table of all courses</th> 
        </tr> 
        <?php $data[0]['Points']="נז";?>
        <?php foreach( $data as $row ) { ?> 
        <tr> 
            <td><?php echo( $row['Year'] ); ?></td>
            <td><?php echo( $row['Semester'] ); ?></td>
            <td><?php echo( $row['Code'] ); ?></td>   
            <td><?php echo( $row['Course'] ); ?></td> 
            <td><?php echo( $row['Points'] ); ?></td> 
            <td><?php echo( $row['Grade'] ); ?></td>
            <td><?php if ($numRow > 0)
                    {
                        for($choise=1;  $choise<6;  $choise++)
                        {
                            echo "<input type='radio' name='difficulty".$numRow."' value='".$choise."'>".$choise."&nbsp;&nbsp;&nbsp;";
                        }
                    }
                ?>
            </td>
        </tr> 
        <?php $numRow++;                }$numRow--; ?> 
    </table> 
    <input name="next" type="submit" value="continue" onclick="return true;">
    </form>

它工作得很好,并且每行上的单选按钮都有不同的值,但问题是,我该如何处理它? 我正在考虑是否将所有选择的 raio 按钮值发送到一个数组?

这部分我也进入了同一页面,因为用户选择了所有:

<?php 

    if(isset($_POST['next']) ) 
    { 
        if(isset($_POST['difficulty2']))
            $difficulty2 = $_POST['difficulty2'];
                    else
            $difficulty2="";
        if(isset($_POST['difficulty1']))
            $difficulty1 = $_POST['difficulty1'];
        else
            $difficulty1="";
        echo "User Has submitted the form and entered this difficulty1 : <b> $difficulty1 </b></br>";
        echo "User Has submitted the form and entered this difficulty2 : <b> $difficulty2 </b></br>";           
    }

    else
    {
?>

我可以得到这样的值,但是有没有比只检查有多少行并循环添加数组中的所有 POST 更清洁、更快的方法? 我希望我说清楚并感谢您的帮助:P

【问题讨论】:

    标签: php forms post dynamic radio


    【解决方案1】:
    for($choise=1;  $choise<6;  $choise++)
    {
        echo "<input type='radio' name='difficulty[".$numRow."]' value='".$choise."'>".$choise."&nbsp;&nbsp;&nbsp;";
    }
    

    然后

    $difficulty = isset($_POST['difficulty'])?$_POST['difficulty']:'';
    
    echo "User Has submitted the form and entered this difficulty1 : <b> {$difficulty[0]} </b></br>";
    echo "User Has submitted the form and entered this difficulty2 : <b> {$difficulty[1]} </b></br>"; 
    

    备注

    action="<?php echo $_SERVER['PHP_SELF']?>"
    

    这对 xss 不利

    【讨论】:

      【解决方案2】:

      试试:

      echo '<input type="radio" name="difficulty['.$numrow.']" value="'.$choise.'" />';
      

      然后 $_POST['difficulty'] 将是一个由行号索引的数组,其值为所选选项。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2010-09-13
        • 1970-01-01
        • 2011-07-28
        • 2016-05-05
        • 1970-01-01
        • 2016-03-31
        • 2013-03-09
        相关资源
        最近更新 更多