douwuying4709 2016-01-19 15:50
浏览 45
已采纳

确定在表单中单击了哪个按钮并在PHP脚本中获取其“唯一值”

It's easy to tell which button was clicked from a set of buttons if each of the button has a unique name, and the name is known at the time of backend processing (when retrieving the data sent from the form, in a php script). Even easier if the set contains not more than 2-3 buttons. But what to do when button names are unique, but not known, and set contains more than 20 buttons. To explain further, I am using an example:

Consider Facebook's add friend module. The site checks current user's (account owner's) city/school or other related data, performs a database query, retrieves a random list of users from same city/school. The template presents the list where each raw has a blue button saying 'Add Friend' at its right corner and user info at its left. No one other than the current user knows which button is going to be clicked. Now assume one funny thing, that non of the fb engineers know what AJAX or JS is. In this case, how to retrieve the unique ID of the user whose ADD FRIEND button was clicked and process database queries in order to send him/her a friend request and insert/update rows accordingly. I couldn't find a way to retrieve the unique value (that could have been assigned as the value of button's 'name' attribute) of the clicked button to process ahead.

  • 写回答

1条回答 默认 最新

  • doumeng9188 2016-01-19 16:14
    关注

    One potential option is to have multiple hidden forms that have all the different 'Add Friend' buttons for example:

    <form method="post" action="addFriend.php">
        <input type="hidden" name="userid" value="123" />
        <input type="submit" name="add" value="Add Friend" />
    </form>
    

    If you have multiple forms like this and just changed the value of the "userid" input you could click on each form and it would go to a page to add that user.

    If you are wanting to do this without a page refresh or relocation then I believe the only way you can do it is through AJAX

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)