使用javascript从数据库显示

Displaying from database using javascript

本文关键字:显示 数据库 javascript 使用      更新时间:2023-09-26

所以我使用这个代码示例来获得一个级联的下拉菜单。但是我想添加居住在一个城市的人的名字。我如何添加,当一个城市被选中?链接到演示- 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