如何使用 php 处理每个搜索结果 2 个单选按钮

发布于 2024-12-03 10:37:55 字数 1461 浏览 3 评论 0原文

当我的用户在我的数据库中搜索游戏时,我向他们展示与他们的搜索词匹配的游戏列表,然后我想向每个项目添加一个按钮,允许用户选择“拥有它”或“想要它” ”然后有一个“添加”按钮,将他们的选择添加到他们的个人资料中。

因为你不能同时拥有它和想要它,我认为单选按钮将是最好的选择。

但现在我不知道如何处理按钮。当我点击搜索中的一个选项时,当我选择拥有或想要另一款游戏时,它将取消选择。

我知道我应该为每个搜索结果创建一个单独的名称表单,但是当将数据发送到我的控制器时我将如何管理数据?也许我需要增加表单名称,然后计算有多少结果并在我的控制器中使用它?

另外,我需要与选择关联的游戏ID,所以我需要为每个选择发送一个包含该数据的隐藏值,

也许我会以错误的方式处理这个问题...

这是我的代码,

echo '<div id="UserSearchGameResults">';
            echo '<form action="#" method="Post">';
            $x = 0;
            while($gamesLike != null)
            {
                $x++;

                echo '<div id="game_search_list_item">'.$x.'. '.$gamesLike['title'].'</div>

                <span class="gamelistblue">
                    <input type="radio" name="haveOrWant" value="have" />Have it
                </span>

                <span class="gamelistorange">
                    <input type="radio" name="haveOrWant" value="want" />Want it
                </span>

                <input type="hidden" name="gameID" value="'.$gamesLike['ID'].'" />';

                $gamesLike = $statement->fetch();
            }
            $statement->closeCursor();

            echo '<br /> <input type="submit" value="Add Game(s)" />';

            echo '</div></form>';   

任何帮助都将受到关于如何处理该主题的

新想法的帮助也欢迎满足我的需求。

When my user searches for game in my DB, I present them with a list of the games that match their search term, and then i want to add a button to each item that allows the user to select "have it" or "want it" then have a single "add" button that adds their selections to their profile.

because you cant have it and want it at the same time, i assumed a radio button would be the best choice.

but now i am lost as to how to handle the buttons. When i click on one selection from the search, it will deselect when i choose have or want on another game.

I understand that i should be creating a separately name form for each search result, but then how would i manage the data when sending it to my controller? Maybe i need incrementing form names and then count how many results and use that in my controller?

Also, i need the gameID associated with the selections so i need to send a hidden value with that data for each selection

maybe i am going about this the wrong way...

heres my code

echo '<div id="UserSearchGameResults">';
            echo '<form action="#" method="Post">';
            $x = 0;
            while($gamesLike != null)
            {
                $x++;

                echo '<div id="game_search_list_item">'.$x.'. '.$gamesLike['title'].'</div>

                <span class="gamelistblue">
                    <input type="radio" name="haveOrWant" value="have" />Have it
                </span>

                <span class="gamelistorange">
                    <input type="radio" name="haveOrWant" value="want" />Want it
                </span>

                <input type="hidden" name="gameID" value="'.$gamesLike['ID'].'" />';

                $gamesLike = $statement->fetch();
            }
            $statement->closeCursor();

            echo '<br /> <input type="submit" value="Add Game(s)" />';

            echo '</div></form>';   

any help is appreciated with the subject

new ideas on how to handle my needs are welcome too.

Dan

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

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

发布评论

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

评论(1

千里故人稀 2024-12-10 10:37:56

使用数组作为输入名称。像这样的东西

<input type="radio" name="game' . $gamesLike['ID'] . '[haveOrWant]" value="have" />Have it

Use arrays for the input names. Something like

<input type="radio" name="game' . $gamesLike['ID'] . '[haveOrWant]" value="have" />Have it
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文