dongtong848825 2014-04-19 12:03
浏览 29
已采纳

使用type =“hidden”获取错误的值

In my application I'm trying to send an invitation for joining a group. Therefore, I want to get user_id of the person you are inviting. At this moment, I get a value but it's always the user_id of the last person of the results, not the person that I choose.

PRINT OUT

   <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条回答 默认 最新

  • doonbfez815298 2014-04-19 12:31
    关注

    An alternative to Barmar's suggestion (which works well), you could also output a separate form for each member, as the submit will only send the values for it's form. This makes more sense to me since you have a submit button for every member:

    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; 
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥15 绘制多分类任务的roc曲线时只画出了一类的roc,其它的auc显示为nan
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?