Javascript Ajax没有连接到数据库?
我已经制作了一个html文件,我希望ajax从中连接到后台的数据库,根据用户预先选择的国家获得所选城市的值。简而言之,当用户在第一个下拉菜单中选择国家时,我只需要使用数据库自动加载所有城市下拉菜单Javascript Ajax没有连接到数据库?,javascript,php,html,ajax,Javascript,Php,Html,Ajax,我已经制作了一个html文件,我希望ajax从中连接到后台的数据库,根据用户预先选择的国家获得所选城市的值。简而言之,当用户在第一个下拉菜单中选择国家时,我只需要使用数据库自动加载所有城市下拉菜单 <!doctype> <html> <head> <title></title> </head> <body> <form method="GET" action="" name="form1"> Cou
<!doctype>
<html>
<head>
<title></title>
</head>
<body>
<form method="GET" action="" name="form1">
Country : <select name="country" onchange="getCity('ind.php? country='+this.value)">
<option value="">Select Country</option>
<option value="1">USA</option>
<option value="2">Canada</option>
</select>
<br />City : <div id="citydiv">
<select name="select">
<option>Select City</option>
</select>
</div>
</form>
<script>
function getCity(strURL)
{
var req = getXMLHTTP(); // fuction to get xmlhttp object
if (req)
{
req.onreadystatechange = function()
{
if (req.readyState == 4) { //data is retrieved from server
if (req.status == 200) { // which reprents ok status
document.getElementById('citydiv').innerHTML=req.responseText;
}
else
{
alert("There was a problem while using XMLHTTP:\n");
}
}
}
req.open("GET", strURL, true); //open url using get method
req.send(null);
}
}
</script>
</body>
</html>
发出AJAX请求不会更改url,除非您在发出AJAX调用后立即使用javascript专门更改url 要使用javascript更改url,可以使用以下内容-
据我所知,getXMLHTTP是一个ajax实用程序。。所以它永远不会“改变”你的url。我认为您误解了您试图使用的工具。@DevDonkey我引用了此链接,因此在本手册中,数据是在页面元素中更改的,而不是url。这是一个旧的(可怕的)教程。它使用的是不推荐使用的mysql驱动程序。
<?php
$country=$_GET['country'];
$link = mysql_connect('localhost', 'root', ''); /change the configuration if required
if($link)
{
echo "connect";
}
if (!$link) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db('country'); //change this if required
$query="select city from city where countryid=$country";
$result=mysql_query($query);
?>
<select name="city">
<option>Select City</option>
<?php while($row=mysql_fetch_array($result)) ?>
<option value><?php=$row['city']?></option>
</select>
location.replace("http://new-url");