使用javascript从数据库显示
Displaying from database using javascript
所以我使用这个代码示例来获得一个级联的下拉菜单。但是我想添加居住在一个城市的人的名字。我如何添加,当一个城市被选中?链接到演示- http://www.infotuts.com/demo/cascaded-drop-down-jquery-php/
——整个代码index . php
<?php
include("connection.php");
?>
<!DOCTYPE html>
<html>
<head>
<title>Cascaded dropdown with jQuery Ajax and PHP | Easyscript4u.com</title>
<link rel="stylesheet" href="style.css" type="text/css" />
</head>
<body>
<div id="container">
<div id="body">
<div class="mhead"><h2>Cascaded dropdown with jQuery Ajax and PHP | Easyscript4u.com</h2></div>
<div id="dropdowns">
<div id="center" class="cascade">
<?php
$sql = "SELECT * FROM tbl_country ORDER BY country_name";
$query = mysqli_query($con, $sql);
?>
<label>Country:
<select name="country" id = "drop1">
<option value="">Please Select</option>
<?php while ($rs = mysqli_fetch_array($query, MYSQLI_ASSOC )) { ?>
<option value="<?php echo $rs["id"]; ?>"><?php echo $rs["country_name"]; ?></option>
<?php } ?>
</select>
</label>
</div>
<div class="cascade" id="state"></div>
<div id="city" class="cascade"></div>
</div>
</div>
</div>
<script src="jquery-1.9.0.min.js"></script>
<script>
$(document).ready(function(){
$("select#drop1").change(function(){
var country_id = $("select#drop1 option:selected").attr('value');
// alert(country_id);
$("#state").html( "" );
$("#city").html( "" );
if (country_id.length > 0 ) {
$.ajax({
type: "POST",
url: "fetch_state.php",
data: "country_id="+country_id,
cache: false,
beforeSend: function () {
$('#state').html('<img src="loader.gif" alt="" width="24" height="24">');
},
success: function(html) {
$("#state").html( html );
}
});
}
});
});
</script>
</body>
</html>
STATE.PHP
<?php
include("connection.php");
$country_id = trim(mysql_escape_string($_POST["country_id"]));
$sql = "SELECT * FROM tbl_state WHERE country_id = ".$country_id ." ORDER BY state_name";
$count = mysqli_num_rows( mysqli_query($con, $sql) );
if ($count > 0 ) {
$query = mysqli_query($con, $sql);
?>
<label>State:
<select name="state" id="drop2">
<option value="">Please Select</option>
<?php while ($rs = mysqli_fetch_array($query, MYSQLI_ASSOC)) { ?>
<option value="<?php echo $rs["id"]; ?>"><?php echo $rs["state_name"]; ?></option>
<?php } ?>
</select>
</label>
<?php
}
?>
<script src="jquery-1.9.0.min.js"></script>
<script>
$(document).ready(function(){
$("select#drop2").change(function(){
var state_id = $("select#drop2 option:selected").attr('value');
// alert(state_id);
if (state_id.length > 0 ) {
$.ajax({
type: "POST",
url: "fetch_city.php",
data: "state_id="+state_id,
cache: false,
beforeSend: function () {
$('#city').html('<img src="loader.gif" alt="" width="24" height="24">');
},
success: function(html) {
$("#city").html( html );
}
});
} else {
$("#city").html( "" );
}
});
});
</script>
CITY.php
<?php
include("connection.php");
$state_id = trim(mysql_escape_string($_POST["state_id"]));
$sql = "SELECT * FROM tbl_city WHERE state_id = ".$state_id ." ORDER BY city_name";
$count = mysqli_num_rows( mysqli_query($con, $sql) );
if ($count > 0 ) {
$query = mysqli_query($con, $sql);
?>
<label>City:
<select name="city" name="box">
<option value="">Please Select</option>
<?php while ($rs = mysqli_fetch_array($query, MYSQLI_ASSOC)) { ?>
<option value="<?php echo $rs["id"]; ?>"><?php echo $rs["city_name"]; ?></option>
<?php } ?>
</select>
</label>
<?php
}
?>
我认为应该有一个javascript运行调用人名在数据库中的特定城市。但我不知道该怎么做。因为我完全是个新手。谢谢你
首先,你的文件应该是:fetch_state.php(而不是state.php)和fetch_city.php(而不是city.php)。
基本上,您需要以下内容:1. 在数据库中创建一个新表"tbl_person",至少包含两个字段:city_id和person_name;
-
通过复制当前文件"fetch_city.php"创建一个名为"fetch_person.php"的新文件,并更改:City -> personstate -> city
-
通过复制当前文件"fetch_state.php"来替换当前的"fetch_city.php",并更改:Drop2 -> drop3州->城市country -> state
相关文章:
- AJAX,用于显示数据库数据
- 使用模式消息/窗口显示数据库记录
- 从下拉框中选择用户信息,并使用进度计数器显示数据库中的选定项目
- 如何在每次在 php 中单击提交时逐个显示数据库中的项目
- 在 HTML 页面上显示数据库表,而无需离开 HTML 页面
- 在 PHP 的 javascript 函数中显示数据库的行值
- 在codeigneter中按类型输入框显示数据库中的数据
- 使用nl2br用换行符显示数据库中的数据
- 如何在引导工具提示中显示数据库消息
- 如何显示数据库中的拆分日期
- 显示数据库中的图像平铺
- 单击时动态显示数据库信息
- 我想显示数据库中的数据.如果我们新添加一个表单数据,我想附加显示的新帖子而不使用代码点火器刷新
- 显示数据库中的标记
- 如何使用jquery和javascript显示数据库中的菜单项
- 显示数据库 MySQL 描述字段中的 125 个单词
- 当用户从下拉列表中选择值时,如何在文本字段中显示数据库值
- MVC - 在“选择缩略图”上,显示数据库中的数据
- Javascript 页面不显示数据库值
- 如何防止下拉显示数据库中的值