Problem w tym, że dochodzę do etapu, gdzie zmienna kraju jest przekazywana go pliku getCities.php . Niestety na tym etapie się zatrzymuję a to informacja z debugera:
"POST http:/.../getCities.php?countryCode=Niemcy [HTTP/1.0 500 Internal Server Error 13509ms]".
Poniżej kody plików użytych w skypcie (oprócz ajax.js) index.php, getCities.php oraz mysql.class.php. Oczywiście u siebie wpisałem poprawną nazwę bazy, usera itp, gdyż czyta mi z bazy nazwy krajów.
Dziękuję za pomoc i pozdrawiam
index.php
Kod
<?PHP echo '<?xml version="1.0" encoding="iso-8859-2"?>'; ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script type="text/javascript" src="ajax.js"></script>
<script type="text/javascript">
var ajax = new sack();
function getCityList(sel)
{
var countryCode = sel.options[sel.selectedIndex].value;
document.getElementById('dhtmlgoodies_city').options.length = 0; // Empty city select box
if(countryCode.length>0){
ajax.requestFile = 'getCities.php?countryCode='+countryCode; // Specifying which file to get
ajax.onCompletion = createCities; // Specify function that will be executed after file has been found
ajax.runAJAX(); // Execute AJAX function
}
}
function createCities()
{
var obj = document.getElementById('dhtmlgoodies_city');
eval(ajax.response); // Executing the response from Ajax as Javascript code
}
</script>
<title></title>
</head>
<body>
<form action="" method="post">
<table>
<tr>
<td>Country: </td>
<td><select id="dhtmlgoodies_country" name="dhtmlgoodies_country" onchange="getCityList(this)">
<option value="">Wybierz</option>
<?PHP
include 'mysql.class.php';
$a = new mysqli_db('localhost', 'user', 'pass', 'db');
$q = $a->query_select("SELECT DISTINCT kraj FROM test");
foreach($q as $i)
{
echo '<option value="'.$i['kraj'].'">'.$i['kraj'].'</option>';
}
$a->__destruct();
?>
</select>
</td>
</tr>
<tr>
<td>City: </td>
<td><select id="dhtmlgoodies_city" name="dhtmlgoodies_city">
</select>
</td>
</tr>
</table>
</form>
</body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script type="text/javascript" src="ajax.js"></script>
<script type="text/javascript">
var ajax = new sack();
function getCityList(sel)
{
var countryCode = sel.options[sel.selectedIndex].value;
document.getElementById('dhtmlgoodies_city').options.length = 0; // Empty city select box
if(countryCode.length>0){
ajax.requestFile = 'getCities.php?countryCode='+countryCode; // Specifying which file to get
ajax.onCompletion = createCities; // Specify function that will be executed after file has been found
ajax.runAJAX(); // Execute AJAX function
}
}
function createCities()
{
var obj = document.getElementById('dhtmlgoodies_city');
eval(ajax.response); // Executing the response from Ajax as Javascript code
}
</script>
<title></title>
</head>
<body>
<form action="" method="post">
<table>
<tr>
<td>Country: </td>
<td><select id="dhtmlgoodies_country" name="dhtmlgoodies_country" onchange="getCityList(this)">
<option value="">Wybierz</option>
<?PHP
include 'mysql.class.php';
$a = new mysqli_db('localhost', 'user', 'pass', 'db');
$q = $a->query_select("SELECT DISTINCT kraj FROM test");
foreach($q as $i)
{
echo '<option value="'.$i['kraj'].'">'.$i['kraj'].'</option>';
}
$a->__destruct();
?>
</select>
</td>
</tr>
<tr>
<td>City: </td>
<td><select id="dhtmlgoodies_city" name="dhtmlgoodies_city">
</select>
</td>
</tr>
</table>
</form>
</body>
</html>
getCities.php
Kod
<?php
if(isset($_GET['countryCode']))
{
include 'mysql.class.php';
$a = new mysqli_db('localhost', 'user', 'pass', 'db');
$q = $a->query_select("SELECT `id` , `miasto` FROM `test` WHERE `kraj` = '".mysql_real_escape_string($_GET['countryCode'])."'");
foreach($q as $i)
{
echo "obj.options[obj.options.length] = new Option('".$i['miasto']."','".$i['id']."');
";
}
$a->__destruct();
}
?>
if(isset($_GET['countryCode']))
{
include 'mysql.class.php';
$a = new mysqli_db('localhost', 'user', 'pass', 'db');
$q = $a->query_select("SELECT `id` , `miasto` FROM `test` WHERE `kraj` = '".mysql_real_escape_string($_GET['countryCode'])."'");
foreach($q as $i)
{
echo "obj.options[obj.options.length] = new Option('".$i['miasto']."','".$i['id']."');
";
}
$a->__destruct();
}
?>
mysql.class.php
Kod
<?php
class mysqli_db
{
public function __construct($host, $user, $password, $dbname)
{
// łączymy się z bazą danych
IF(!$this->mysqli = new mysqli($host, $user, $password))
{
$this->error = true;
// W przypadku niepowodzenia połączenia wygeneruj wyjątek
throw new Exception('Błąd Połączenia z Bazą Danych - '.$this->mysqli->error, $this->mysqli->errno);
}
// wybieramy bazę danych
IF(!$this->mysqli->select_db($dbname))
{
$this->error = true;
// W przypadku niepowodzenia wybrania bazy wygeneruj wyjątek
throw new Exception('Nie można wybrać bazy danych - '.$this->mysqli->error, $this->mysqli->errno);
}
// ustawiamy "tryb" transakcji dla tabel InnoDB
$this->mysqli->autocommit(false);
$this->mysqli->query('SET AUTOCOMMIT = 0');
$this->mysqli->query('BEGIN');
}
// Wykonywanie zapytań nie zwracających wartości (nie-Select)
public function query($query)
{
IF(!ereg('SELECT', $query) and !$this->error)
{
IF(!$result = $this->mysqli->query($query))
{
$this->error = true;
throw new Exception('Błąd wykonania zapytania - ('.$query.') - '.$this->mysqli->error, $this->mysqli->errno);
}
else
{
return true;
}
}
}
// Zapytania z SELECT zwrócą nam od razu tablicę asocjacyjną z wynikami
public function query_select($query)
{
IF(!$this->error)
{
IF(!$result = $this->mysqli->query($query))
{
$this->error = true;
throw new Exception('Błąd wykonania zapytania - ('.$query.') - '.$this->mysqli->error, $this->mysqli->errno);
}
while($row = $result->fetch_assoc())
{
$return[] = $row;
}
unset($result);
unset($row);
return $return;
}
}
// ID pola autoincrement użyte w ostatnim zapytaniu INSERT
public function insert_id()
{
return $this->mysqli->insert_id;
}
public function escape($string)
{
return $this->mysqli->real_escape_string($string);
}
// Destruktor, w przypadku błędów wszystkie zmiany będą cofnięte
public function __destruct()
{
IF(!$this->error)
{
$this->mysqli->query('COMMIT');
}
else
{
$this->mysqli->query('ROLLBACK');
}
unset($this->mysqli);
unset($this->error);
}
}
?>
class mysqli_db
{
public function __construct($host, $user, $password, $dbname)
{
// łączymy się z bazą danych
IF(!$this->mysqli = new mysqli($host, $user, $password))
{
$this->error = true;
// W przypadku niepowodzenia połączenia wygeneruj wyjątek
throw new Exception('Błąd Połączenia z Bazą Danych - '.$this->mysqli->error, $this->mysqli->errno);
}
// wybieramy bazę danych
IF(!$this->mysqli->select_db($dbname))
{
$this->error = true;
// W przypadku niepowodzenia wybrania bazy wygeneruj wyjątek
throw new Exception('Nie można wybrać bazy danych - '.$this->mysqli->error, $this->mysqli->errno);
}
// ustawiamy "tryb" transakcji dla tabel InnoDB
$this->mysqli->autocommit(false);
$this->mysqli->query('SET AUTOCOMMIT = 0');
$this->mysqli->query('BEGIN');
}
// Wykonywanie zapytań nie zwracających wartości (nie-Select)
public function query($query)
{
IF(!ereg('SELECT', $query) and !$this->error)
{
IF(!$result = $this->mysqli->query($query))
{
$this->error = true;
throw new Exception('Błąd wykonania zapytania - ('.$query.') - '.$this->mysqli->error, $this->mysqli->errno);
}
else
{
return true;
}
}
}
// Zapytania z SELECT zwrócą nam od razu tablicę asocjacyjną z wynikami
public function query_select($query)
{
IF(!$this->error)
{
IF(!$result = $this->mysqli->query($query))
{
$this->error = true;
throw new Exception('Błąd wykonania zapytania - ('.$query.') - '.$this->mysqli->error, $this->mysqli->errno);
}
while($row = $result->fetch_assoc())
{
$return[] = $row;
}
unset($result);
unset($row);
return $return;
}
}
// ID pola autoincrement użyte w ostatnim zapytaniu INSERT
public function insert_id()
{
return $this->mysqli->insert_id;
}
public function escape($string)
{
return $this->mysqli->real_escape_string($string);
}
// Destruktor, w przypadku błędów wszystkie zmiany będą cofnięte
public function __destruct()
{
IF(!$this->error)
{
$this->mysqli->query('COMMIT');
}
else
{
$this->mysqli->query('ROLLBACK');
}
unset($this->mysqli);
unset($this->error);
}
}
?>