动态生成的复选框的值

发布于 2024-12-10 16:44:59 字数 2575 浏览 1 评论 0原文

我试图获取 PHP 中动态创建的一组复选框的值,但显然我无法获取它。源代码如下。

“managestaff.php”页面将允许通过姓名搜索员工,并抛出一个带有复选框的姓名列表,供管理员检查,然后单击底部的“删除”按钮删除正在检查的员工。

删除将在“deletestaff.php”上完成,因为“managestaff.php”上的“删除”按钮只是将这些值转发到“deletestaff.php”以执行员工的删除工作。

“managestaff.php”页面代码:

<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”页面代码:

<?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 !';
}
?>

“deletestaff.php”目前不执行删除操作,因为它是测试页面。

我得到的当前输出是“未找到 delstaffform 的提交!”。

感谢您的解决方案。

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 = '   ';
$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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浊酒尽余欢 2024-12-17 16:44:59

试试这个:

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

print_r 你的 $_POST ,你会看到它很好地将你的提交内容粘到了一个数组中。

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.

左秋 2024-12-17 16:44:59
<?php
if (isset($_POST['delstaff']) && is_array($_POST['delstaff'])) {

    echo 'Submission of delstaffform FOUND !';

    $array = $_POST["delstaff"];

    foreach($array as $value){
        echo "<br>Value: ".$value."<br>";
    }

} else {
    echo 'Submission of delstaffform NOT FOUND !';
}
?>

我自己找到了答案,但尽管如此,你还是有帮助的:D。多谢。

<?php
if (isset($_POST['delstaff']) && is_array($_POST['delstaff'])) {

    echo 'Submission of delstaffform FOUND !';

    $array = $_POST["delstaff"];

    foreach($array as $value){
        echo "<br>Value: ".$value."<br>";
    }

} else {
    echo 'Submission of delstaffform NOT FOUND !';
}
?>

Found the answer on my own but nevertheless you are helpful :D . Thanks a lot.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文