[英]populate dropdown list using jquery load event
I want to populate dropdown value from mysql table.I used load event in html page,but i don't know this code doesn't work. 我想从mysql table中填充下拉值。我在html页面中使用了load事件,但是我不知道此代码不起作用。
HTML PAGE 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 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>";
}
}
?>
If you want to do the logic in the front end using jquery , you can follow this example : 如果要使用jquery在前端进行逻辑处理,则可以遵循以下示例:
jQuery: Best practice to populate drop down? jQuery:填充下拉列表的最佳实践?
This way you can return your php array using json_encode()
and in you jquery function you access it as an object, like in the above example. 这样,您可以使用
json_encode()
返回php数组,并在jquery函数中将其作为对象访问,如上例所示。
change this 改变这个
<select id="select1"></select>
to this 对此
<div id="select1"></div>
and add the select tags to the php 并将select标签添加到php
$result1 = mysql_query("SELECT column1 FROM information");
echo "<select>";
while($row=mysql_fetch_array($result1))
{
if($row['column1']!=NULL)
{
echo "<option value='$row[column1]'>$row[column1]</option>";
}
}
echo "</select>";
and give the button an id 并给按钮一个ID
<button id="button">Get External Content</button>
In HTML
instead of including <select>
try this, 在
HTML
不要包含<select>
试试这个,
<div id="select1"></div>
And in php
try this, 在
php
试试这个
echo "<select>";
while($row=mysql_fetch_array($result1))
{
if($row['column1']!=NULL)
{
echo "<option value='$row[column1]'>$row[column1]</option>";
}
}
echo "</select>";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.