gpt4 book ai didi

php - 根据 MySQL 给出的数据更改一个下拉列表

转载 作者:行者123 更新时间:2023-11-29 07:25:33 25 4
gpt4 key购买 nike

我需要在下拉列表中获取一些数据,然后在选择特定类型后,我需要另一个下拉列表来从 MySQL 获取特定类型的值。

I have this link工作正常,但我无法修改它以接受数据库中的值。

我的第一个下拉列表栏是:

Refrigerator

Microwave

TVs

Heaters

然后,当我单击其中一个时,我将在另一个下拉列表中选择一个新数据,就像我选择电视一样,第二个下拉列表将带来新数据,例如:

LG

SAMSUNG

VESTEL

...

感谢任何帮助。

最佳答案

你需要使用像AJAX这样的东西,

Jquery/Ajax 代码

$(document).ready(function() {

$('#id-of-the-first-drop-list').change(function() {

// get the form information
// this can be done in many ways but we are going to put the form
// data into a data object
var formData = {
'selectedValue' : $('#id-of-the-first-drop-list').val()
};

// send the data via Ajax
$.ajax({
type : 'POST', // the method we want to use to send the data
url : 'get-data-from-database.php', // the url where we want to
// send the data
data : formData, // the data object we created
dataType : 'json', // what type of data we want to get back
encode : true
})
// execute function when data has been sent and server
// code is processed
.done(function(data) {
// HERE ADD THE CODE THAT UPDATES THE OTHER DROPLIST
// I BELIEVE YOU WILL BE ABLE TO ACCESS THE DATA LIKE THIS
// data[0], data[1]... TO GET THE VALUE

// I got this from somewhere else on stackoverflow
var myOptions = {
val1 : data[0],
val2 : data[1]
};
var mySelect = $('#id-of-the-second-drop-list');
$.each(myOptions, function(val, text) {
mySelect.append(
$('<option></option>').val(val).html(text)
);
});
});

});

});

现在这是将从数据库获取数据的服务器端代码

PHP代码

<?php
$data = $_POST['selectedValue'];

// Connect to database
// Use the data to get the new information
$query = "SELECT * FROM table_name WHERE column_name = '$data'"
// MySQL
$results = mysql_query($query);

$data = array();
$i = 0;
while($row = mysql_fetch_array($results)) {
data[i] = row['column_name'];
$i++;
}

echo json_encode($data);
?>

此代码未经测试,可能有错误,但这是总体思路。我希望它有帮助。

关于php - 根据 MySQL 给出的数据更改一个下拉列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34703322/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com