drd99007
drd99007
2012-03-23 12:21
浏览 64
已采纳

如何访问从mysql数据库填充的php下拉列表中显示的值

Firstly, let me say that I've been searching the Internet, rewriting scripts and I still can't seem to find a solution to my problem or even one that could possibly help. I've even searched this site for similar questions and the focus seems to be on actually displaying the database values but what I want is to access those values. Or rather let me put it like this, I have a drop down that is generated through php and it contains data retrieved from mysql... all that works fine no errors.

My current problem is that when a user select an option from my dropdown, I want to use that value to do more operations based on the user's selection. Please help, I've been at it for a week with no avail. Thanks in advance. If you need samples of my code please tell me.

图片转代码服务由CSDN问答提供 功能建议

首先,让我说我一直在搜索互联网,重写脚本,我似乎仍然无法 找到我的问题的解决方案,甚至可能有帮助的解决方案。 我甚至在这个网站上搜索了类似的问题,重点似乎是实际显示数据库值,但我想要的是访问这些值。 或者更确切地说,让我这样说,我有一个通过php生成的下拉列表,它包含从mysql中检索到的数据......一切正常,没有错误。

我当前的问题是当用户从我的下拉列表中选择一个选项时,我想根据用户的选择使用该值来执行更多操作。 请帮助,我已经在这一周没有用了。 提前致谢。 如果您需要我的代码示例,请告诉我。

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • doulierong0334
    doulierong0334 2012-03-23 12:34
    已采纳

    Your problem is not that easy since php only runs on the server and does not directly get any information about what a user is clicking and selecting in his browser.

    One possible solution (and the most common one nowadays) is to load any further information asynchronously via AJAX as soon as the user selects something in your dropdown. jQuery is a neat framework that can help you with this.

    An other method is to automatically submit the form as soon as something is selected and read the selected value with php ($_POST variable). With this you could then easily generate a more custom website according to the selection that was made. Code could look like this:

    <form method="post" action="" name="myform">
    <select name="myfield" onchange="this.form.submit()">
    <option .... >
    ...
    </select>
    </form>
    

    (Note that the form is submitted when the onchange event is triggered)

    This method clearly has the disadvantage that the whole page has to be reloaded whenever a selection is made. With the AJAX approach the user does not even notice that some additional information was fetched form the server. (Except a small waiting time that may occur)

    What you want to do depends on your own decisioin. However the first approach may need you to get into some javascript programming. But the result is certainly more comfortable for the user...

    点赞 评论
  • doupoji3856
    doupoji3856 2012-03-23 12:25

    You could make a callback function on the onselect Event. This is the easiest with jQuery in my experience.

    http://api.jquery.com/select/

    点赞 评论
  • duanbianweng5353
    duanbianweng5353 2012-03-23 12:30

    Wrap your select in a form which had a post of type 'get'. This will post to the same page you are in and then you can use the get parameter on page refresh to load your second list

    点赞 评论

相关推荐