动态填充下拉列表PHP MySQL(Dynamic Populated Drop Down List PHP MySQL)

编程入门 行业动态 更新时间:2024-10-14 10:38:31
动态填充下拉列表PHP MySQL(Dynamic Populated Drop Down List 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对象,概述了可能性,然后我将静态内容交换为静态内容。

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

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发送“通过电话线”的数据。 没有教程的空间,我想我可以安全地将你链接到这个教程。

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.

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 ); }

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.

更多推荐

本文发布于:2023-08-03 09:18:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1385433.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:动态   列表   PHP   MySQL   List

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!