Vlas1
Новичок
Динамический select на AJAX
Добрый день!
Скажите, что я не так делаю:
файл index.php:
кода для обработки на сервере :
скрипт:
В результате ошибка и не выводится список городов
Добрый день!
Скажите, что я не так делаю:
файл index.php:
PHP:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<?php
$mysql_host = "localhost"; // MySQL host name (usually:localhost)
$mysql_user = "ххххх"; // MySQL username
$mysql_pass = "ххххх"; // MySQL password
$mysql_base = "ххххх"; // MySQL database
mysql_connect($mysql_host, $mysql_user, $mysql_pass) or die(mysql_error());
mysql_select_db($mysql_base) or die(mysql_error());
?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Города и регионы</title>
<script type="text/javascript" src="quickstart.js"></script>
</head>
<body onload='process()'>
<table>
<form name=form1 method="get">
<?php
$zapros="SELECT DISTINCT id_reg, n_reg FROM city ORDER by n_reg";
$result = mysql_query($zapros) or die(mysql_error());
echo "<tr><td colspan=2>Область</td></tr><tr><td colspan=2>
<select name=reg onChange=\"goReg();\" id=\"reg\">";
while ($a = mysql_fetch_array($result)) {
echo "<option";
if($a['id_reg']==5) echo " selected";
echo " value=".$a['id_reg'].">".$a['n_reg']."</option>";
}
echo "</select></td></tr>"; /* */
echo "<tr><td colspan=2>Город</td></tr><tr><td colspan=2>";
?>
<div id="divMessage"/>
</td></tr>
</form>
</table>
</body>
</html>
PHP:
<?php
header('Content-Type: text/xml');
echo '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>';
$mysql_host = "localhost"; // MySQL host name (usually:localhost)
$mysql_user = "ххххх"; // MySQL username
$mysql_pass = "ххххх"; // MySQL password
$mysql_base = "ххххх"; // MySQL database
mysql_connect($mysql_host, $mysql_user, $mysql_pass) or die(mysql_error());
mysql_select_db($mysql_base) or die(mysql_error());
$reg = $_GET['reg'];
echo '<response>';
$zapros1="SELECT DISTINCT id_city, n_city FROM city WHERE id_reg='$reg' ORDER by n_city";
$result1 = mysql_query($zapros1) or die(mysql_error());
$qweqwe="";
while ($a1 = mysql_fetch_array($result1)) {
$qweqwe=$qweqwe."<option value=".$a1['id_city'].">".$a1['n_city']."</option>";
}
echo htmlentities("<select name=city>");
echo htmlentities($qweqwe);
echo htmlentities("</select>");
echo '</response>';
?>
PHP:
var xmlHttp = createXmlHttpRequestObject();
function createXmlHttpRequestObject(){
var xmlHttp;
if(window.ActiveXObject){
try {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e){
xmlHttp = false;
}
}else{
try{
xmlHttp = new XMLHttpRequest();
}
catch (e){
xmlHttp = false;
}
}
if (!xmlHttp) alert("Error creating the XMLHttpRequest object.");
else return xmlHttp;
}
function process(){
if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0){
reg = encodeURIComponent(document.getElementById("reg").value);
xmlHttp.open("GET", "quickstart.php?reg=" + reg, true);
xmlHttp.onreadystatechange = handleServerResponse;
xmlHttp.send(null);
} else setTimeout('process()', 1000);
}
function handleServerResponse(){
if (xmlHttp.readyState == 4) {
if (xmlHttp.status == 200) {
xmlResponse = xmlHttp.responseXML;
xmlDocumentElement = xmlResponse.documentElement;
helloMessage = xmlDocumentElement.firstChild.data;
document.getElementById("divMessage").innerHTML = '<i>' + helloMessage + '</i>';
setTimeout('process()', 1000);
} else {
alert("There was a problem accessing the server: " + xmlHttp.statusText);
}
}
}
function goReg(){
var url="quickstart.php?reg="+document.forms['form1'].reg.value;
createXmlHttpRequestObject(url);
}