使用type =" hidden"获取错误的值

时间:2014-04-19 12:03:08

标签: php function types

在我的申请表中,我正在尝试发送加入群组的邀请。因此,我希望获得您邀请的人user_id。此时,我得到一个值,但它始终是结果最后一个人的user_id,而不是我选择的人。

打印

   <div id="InviteGroupMembers">
            <form action="<?php echo $_SERVER['PHP_SELF'] . "?group_id=" .$group_id; ?>" method="post">
                <div >
                    <input type="text" name="btnSearch" placeholder="Add people to group"/>
                </div>

                <div>
                    <button type="submit"  name="">Add</button>
                </div>
                </form>

            <form action="<?php echo $_SERVER['PHP_SELF'] . "?group_id=" .$group_id; ?>" method="post">
                    <?php
                    if (is_object($searchresult))
                        while ($row = $searchresult -> fetch_array()) {
                            echo "<div><p class='searchresults'><img src='uploads/" . $row['avatar'] . " " . "' alt='' />" .  $row['surname'] . " " . $row['name'] . " " . "<input type='submit' name='btnAddMember'class='addFriendToGroup' value='' /><input type='hidden' name='user_id' value='" . $row['user_id'] . "'/></p></div>"; }         

                    ?>
                </form>


        </div>

PHP

if (isset($_POST["btnSearch"])) {
    try {
        $searchinput = mysql_real_escape_string($_POST['btnSearch']);
        $searchresult = $user -> Search($searchinput);
    } catch(exception $e) {
        $feedback = $e -> getMessage("no results");
    }
}

if (isset($_POST["btnAddMember"])) {
    try{
        $group_receiver_id = mysql_real_escape_string($_POST['user_id']);
        var_dump($group_receiver_id);
    }
    catch(exception $e) {
        $feedback = $e -> getMessage("no results");
    }

}

1 个答案:

答案 0 :(得分:2)

作为Barmar建议的替代方案(效果很好),您还可以为每个成员输出单独的表单,因为提交只会发送其表单的值。这对我来说更有意义,因为每个成员都有一个提交按钮:

if (is_object($searchresult)) {
    while ($row = $searchresult -> fetch_array()) {
        echo <<<EOT
        <form action="{$_SERVER['PHP_SELF']}?group_id={$group_id}" method="post"> .
        <p class="searchresults">
        <img src="uploads/{$row['avatar']}" />{$row['surname']} {$row['name']}
        <input type="submit" name="btnAddMember" class="addFriendToGroup" value="" />
        <input type="hidden" name="user_id" value="{$row['user_id']}" />
        </p></form>
EOT; 
    }
}