gpt4 book ai didi

php - 使用 jquery load 事件填充下拉列表

转载 作者:行者123 更新时间:2023-11-29 14:13:31 26 4
gpt4 key购买 nike

我想从 mysql 表填充下拉值。我使用了 load 事件 在html页面中,但我不知道这段代码不起作用。

HTML 页面

<!DOCTYPE html>
<html>
<head>
<script src="jquery.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("#select1").load("se2.php");
});
});
</script>
</head>
<body>

<select id="select1"></select>
<button>Get External Content</button>

</body>
</html>

se2.php

 <?php
$dbhandle = mysql_connect("localhost","root","")
or die("Unable to connect to MySQL");
$selected = mysql_select_db("student",$dbhandle)
or die("Could not select examples");
$result1 = mysql_query("SELECT column1 FROM information");
while($row=mysql_fetch_array($result1))
{
if($row['column1']!=NULL)
{
echo "<option value='$row[column1]'>$row[column1]</option>";
}
}
?>

最佳答案

如果您想使用jquery在前端执行逻辑,您可以按照以下示例进行操作:

jQuery: Best practice to populate drop down?

这样,您可以使用 json_encode() 返回 php 数组,并在 jquery 函数中将其作为对象访问,如上面的示例所示。

关于php - 使用 jquery load 事件填充下拉列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13079857/

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