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

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

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀