php-动态创建单选按钮句柄

时间:2013-04-22 09:44:02

标签: php forms post dynamic radio

所以..我有一个上传页面,我可以上传一个充满数据的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

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

尝试:

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

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