dongzhuang2030 2011-10-19 05:23
浏览 13

来自动态生成的复选框的值

I am trying to get the values of a dynamically created set of checkboxes in PHP but apparently I couldn't get it. The source codes are below.

The "managestaff.php" page would allow searching for staff via their names and throws out a list of names with checkboxes for the admin to check them and click on a "delete" button at the bottom to delete the staff whom are being checked.

The deletion would be done on "deletestaff.php" as the "delete" button on "managestaff.php" simply forwards these values to "deletestaff.php" to do deletion work of the staff.

"managestaff.php" page codes:

<b><h3>Manage Staff</h3></b><br/>

<form action="managestaff.php" method="POST">
<input name="form" type="hidden" id="form" value="true">
<table width=300>
    <tr>
        <td width=112>Staff Name: </td>
        <td width=188><input type="text" class="textfield" name="sname" /><br/></td>
    </tr>
</table><br/>
<input type="submit" value="submit" />
</form>
<?php
if (isset($_POST['form']) && (isset($_POST['sname'])) && $_POST['form'] == 'true') {

$space = '&nbsp;&nbsp;&nbsp;';
$staffname = mysql_real_escape_string($_POST['sname']);
$query = 'SELECT * from staff where staffname like \'%' . $staffname . '%\'';

$result = mysql_query($query) or die(mysql_error());

if (mysql_num_rows($result) != 0) {
    echo '<br><br>';
    echo '<table>';
    echo '<tr><th>Staff ID' . $space . '</th><th>Staff Name' . $space . '</th></tr>';
    echo '<form action="deletestaff.php" method="POST">';
    echo '<input name="delstaffform" type="hidden">';
    while ($row = mysql_fetch_array($result)) {
        echo '<tr>';
        echo '<td>' . $row['staffid'] . '</td><td>' . $row['staffname'] . '</td>';

        // :Begin - dynamic checkbox generation for deleting staff
        echo '<td>';
        echo '<input type="checkbox" name="delstaff" value="' . $row['staffid'] . '"                 />';
        echo '</td>';
        // :End
        echo '</tr>';
    }
    echo '<tr align="right"><td colspan="3"><input type="submit" value="delete"/></td></tr>';
    echo '</form>';
    echo '</table>';

}
}
?>

"deletestaff.php" page codes:

<?php
print_r('POST: ' . $_POST);

echo '<br>';

if (isset($_POST['delstaffform']) && isset($HTTP_POST_VARS)) {
    echo 'Submission of delstaffform FOUND !';
    echo 'Staff to delete' . $HTTP_POST_VARS['delstaff'];
}
else{
    echo 'Submission of delstaffform NOT FOUND !';
}
?>

The "deletestaff.php" doesn't do delete for now as it's a test page.

The current output I get is "Submission of delstaffform NOT FOUND !".

Thanks for the solutions.

  • 写回答

2条回答 默认 最新

  • dqz30992 2011-10-19 05:59
    关注

    Try this:

    <input type="checkbox" name="delstaff[]" value="' . $row['staffid'] . '"/>';
    

    print_r your $_POST and you'll see it sticks your submissions nicely into an array for you.

    评论

报告相同问题?