PHP - 获取仅在提交后在循环表中检查的值

时间:2012-11-11 05:31:15

标签: php mysql

基本上我的表单必须为在页面中加载的表中检查的注册用户添加信用。当用户单击提交按钮时,被检查的注册用户将具有额外的信用。我的问题是我不知道如何检索循环表中检查的行。这是我的代码。

<form name="blah" method="POST" action="">
<?php
include("mysql_config.php");
// INITIAL OUTPUT WHEN THE PAGE IS LOADED
echo "<table class='sample'><tr>
<td></td>
<td><b>User ID</b></td>
<td><b>Username</b></td>
<td><b>First Name</b></td>
<td><b>Last Name</b></td>
<td><b>Credits</b></td>
</tr>";
$q = "SELECT * FROM users_credits";
$res = mysql_query($q);
$num_rows = mysql_num_rows($res);
for($i=0; $i<$num_rows; $i++){
    $db_field = mysql_fetch_assoc($res);
    $reg_id = $db_field['reg_id'];
    $credits = $db_field['credits'];
    $q2 = "SELECT username, fn, ln FROM reg_users WHERE reg_id = $reg_id";
    $res2 = mysql_query($q2);
    $db_field2 = mysql_fetch_assoc($res2);
    $username = $db_field2['username'];
    $fn = $db_field2['fn'];
    $ln = $db_field2['ln'];
    echo "<tr>
        <td><input type='checkbox' value='$i' name='add_credit'></td>
        <td>$reg_id</td>
        <td>$username</td>
        <td>$fn</td>
        <td>$ln</td>
        <td>$credits</td>
        </tr>";
}
?>
<input type="submit" name="save" value="Save" >
</form>

提前感谢那些帮助我的人。 :)

3 个答案:

答案 0 :(得分:0)

单击按钮时编写要调用的Javascript函数 -

function updateFilter(){
        var tmp="";
        checkboxes = document.getElementsByName('<checkbox id>');
        for(var i in checkboxes){
            if(checkboxes[i].checked)
                tmp=tmp + "," + checkboxes[i].value;
        }
        tmp=tmp.substring(1);   
        parent.window.location.href="<file to open>.php?p=" + tmp;
    }

您的HTML for复选框看起来像这样 -

<input type="checkbox" name="p<checkbox id>" value="<?php echo $row['Product_ID']; ?>"

理想情况下,这会连接复选框的所有值并将其作为参数传递(在本例中为PHP),并且可以在下一页中解码为

$r=mysql_query("select * from product_master where Product_ID in (" . $p.")");

或使用拆分。我建议复选框的值应该是ID而不是文本。

答案 1 :(得分:0)

将您的复选框更改为:

<input type='checkbox' value='$reg_id' name='add_credit[]'>

然后,当您发布到您的操作时,您将获得所有已检查用户的reg_id列表。它将是一个可以使用$ _POST ['add_credit']

访问的数组

答案 2 :(得分:0)

请将您的页面更新为:

<form name="blah" method="POST" action="update.php">
<?php
include("mysql_config.php");
// INITIAL OUTPUT WHEN THE PAGE IS LOADED
echo "<table class='sample'><tr>
<td></td>
<td><b>User ID</b></td>
<td><b>Username</b></td>
<td><b>First Name</b></td>
<td><b>Last Name</b></td>
<td><b>Credits</b></td>
</tr>";
$q = "SELECT * FROM users_credits";
$res = mysql_query($q);
$num_rows = mysql_num_rows($res);

    while($db_field = mysql_fetch_assoc($res))
    {
    $reg_id = $db_field['reg_id'];
    $credits = $db_field['credits'];
    $q2 = "SELECT username, fn, ln FROM reg_users WHERE reg_id = $reg_id";
    $res2 = mysql_query($q2);
    $db_field2 = mysql_fetch_assoc($res2);
    $username = $db_field2['username'];
    $fn = $db_field2['fn'];
    $ln = $db_field2['ln'];
    echo "<tr>
        <td><input type='checkbox' value='".$reg_id."' name='add_credit[]'></td>
        <td>$reg_id</td>
        <td>$username</td>
        <td>$fn</td>
        <td>$ln</td>
        <td>$credits</td>
        </tr>";
}
?>

将update.php中的表单操作设为

<?php
$users=$_REQUEST['add_credit'];

if(is_array($users))
{
foreach($users as $key=>$val)
{

echo $val;

}

}
?>

在这里,您将获得数组中的用户ID。