Search code examples
phpformsradio-button

php- dynamically created radio buttons handle


so.. i've got an upload page where i can upload an xml table filled with data. im handling it on the page itself (not sending it to any db- after the page is closed its erased) i dont know how many rows will be uploaded everytime but its not suppose to be larger than 100.

now, i got this part, as a form that gets all of the data uploaded and the user needs to pick a value in the radio button created for every row (value of radio buttons: 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>

it works good and for every gets diffrent value for radio buttons on each row good, but the problem is, how do i handle it? i was thinking about maybe sending all of the picked raio buttons values to an array?

i also got in the same page this part, for after the user picked all:

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

i can get the values like that, but is there a cleaner, faster way than just checking how many rows are there and looping for adding all POST in an array? i hope i made myself clear and thanks for the help :P


Solution

  • for($choise=1;  $choise<6;  $choise++)
    {
        echo "<input type='radio' name='difficulty[".$numRow."]' value='".$choise."'>".$choise."&nbsp;&nbsp;&nbsp;";
    }
    

    then

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

    remark

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

    this not good for xss