动态填充下拉列表PHP MySQL [英] Dynamic Populated Drop Down List PHP MySQL

查看:69
本文介绍了动态填充下拉列表PHP MySQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个下拉列表,第二个基于用户在第一个下拉列表中选择的列表。我将使用Javascript动态更改第二个下拉列表。但是现在问题来了。此信息将存储在用户的个人资料中。因此,理想情况下,我希望能够从MySQL数据库中提取此信息并将其显示在PHP页面上。当我从MySQl填充列表时,如何加载正确的动态菜单。我该如何以自己想要的方式完成此操作。我不认为javascript在这种情况下会行得通吗?

I have two Drop Down Lists, the second ones are based off which one the users selects in the first drop down. I was going to use Javascript to dynamically change the second drop down. But now here comes the problem. This information is going to be stored on a user's profile. So ideally I would like to be able to pull this information out of MySQL database and display it on a PHP page. How would I go about loading the proper dynamic menu when I populate the list from MySQl. How can I accomplish this the way I want. I dont think javascript would work in this case would it?

推荐答案

如果您不想刷新页面,那么答案是 JavaScript。现在,您有几个选择。就个人而言,我更喜欢预先加载更多内容,因此我保留了一个JSON对象,概述了各种可能性,然后将静态内容交换为静态内容。

If you don't want a page refresh, then the answer is "JavaScript". Now, you have a couple of options there. Personally, I prefer to load more up front, so I keep a JSON object which outlines the possibilities and then I swap static content for static content.

这意味着onchange我会有类似的东西:

This would mean that onchange I would have something like:

var first = document.getElementById( "id of first select" )
var items = possibilities[ 
    first.value // you can also do things with selectedIndex and options if needs.
];

var second = document.getElementById( "id of second select" )
for( var i in items )
{
    var opt = document.createElement('option');
    opt.setAttribute('value', items[ it ] );
    second.appendChild( opt );
} 

您的另一种选择是通过网络发送数据并使其正常工作通过AJAX。我没有足够的教学空间,我可以安全地将您链接到

Your other option is to send data "over the wire" and get it working through AJAX. Not having room for a tutorial, I think I can safely link you to this one.

这篇关于动态填充下拉列表PHP MySQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆