Hi php masters..
i manage to use an ajax combobox to shows bus fares and destination on my site and it works great in xampp..
problem start when i tried to upload the site to my host server (linux)..
the combobox contains 4 .php scripts and 1 .js script
there are 3 tables in the mysql database; origin (asal), destination (tujuan) and price (harga).
i already googling to solve my problem but still no result.. :(
thanks for you help.... :D
so here goes..
var xmlHttp = buatObjekXmlHttp();
function buatObjekXmlHttp()
{
var obj = null;
if (window.ActiveXObject)
obj = new ActiveXObject("Microsoft.XMLHTTP");
else
if (window.XMLHttpRequest)
obj = new XMLHttpRequest();
// Check xmlHttp content
if (obj == null)
document.write(
"Browser tidak mendukung XMLHttpRequest");
return obj;
}
function ambilData(sumber_data, id_elemen)
{
if (xmlHttp != null)
{
var obj = document.getElementById(id_elemen);
xmlHttp.open("GET", sumber_data, true);
xmlHttp.onreadystatechange = function ()
{
if (xmlHttp.readyState == 4 &&
xmlHttp.status == 200)
{
obj.innerHTML = xmlHttp.responseText;
}
}
xmlHttp.send(null);
}
}
infotujuan.php
<html>
<head>
<title>Mencari Data Tujuan</title>
<script type="text/javascript" src="ajax.js">
</script>
<script type="text/javascript">
function tampilkanPilihanNegara()
{
var elemen_benua = document.getElementById("id_benua");
var nama_benua = elemen_benua.value;
if (nama_benua == '--')
{
var lokasi_negara =
document.getElementById("id_lokasi_negara");
lokasi_negara.innerHTML = "";
var lokasi_ket =
document.getElementById("id_lokasi_keterangan");
lokasi_ket.innerHTML = "";
return;
}
var url = "pilihtujuan.php?benua=" + nama_benua ;
ambilData(url, "id_lokasi_negara");
}
function tampilkanInfoNegara()
{
var elemen_id_negara = document.getElementById("id_negara");
var id_negara = elemen_id_negara.value;
if (id_negara == '--')
{
var lokasi_ket =
document.getElementById("id_lokasi_keterangan");
lokasi_ket.innerHTML = "";
return;
}
var url = "kettujuan.php?id=" + id_negara;
ambilData(url, "id_lokasi_keterangan");
}
</script>
</head>
<body>
<form>
<table>
<tr>
<td>Asal:</td>
<td>
<select name="selectbenua" id="id_benua"
onchange="tampilkanPilihanNegara();">
<?php
require_once("koneksi.php");
$sql = "SELECT `lokasiasal`.`asal`\n"
. "FROM `lokasiasal`\n"
. "ORDER BY `lokasiasal`.`asal`";
$hasil = mysql_query($sql);
if ($hasil)
{
print("<option value=\"--\" selected=\"selected\">" .
"-- Pilih Asal --</option>\n");
while ($baris = mysql_fetch_row($hasil))
{
$benua = $baris[0];
print("<option value=\"$benua\">$benua</option>\n");
}
}
mysql_close($id_mysql);
?>
</select>
</td>
</tr>
<tr>
<td>Tujuan:</td>
<td><span id="id_lokasi_negara"></span></td>
</tr>
<tr>
<td>Keterangan:</td>
<td></td>
</tr>
<tr>
<td><span id="id_lokasi_keterangan"></span></td>
</tr>
</table>
</form>
<div id="id_info"></div>
</body>
</html>
kettujuan.php
<?php
header("Cache-Control: no-cache, must-revalidates");
header("Expires: Mon, 26 Jul 1997 00:00:00 GMT");
require_once("koneksi.php");
$id_negara = $_GET["id"]; // Get URL Variable
$sql = "SELECT `lokasiasal`.`asal`, `lokasitujuan`.`tujuan`, `tabelharga`.`harga`\n"
. "FROM `lokasiasal`\n"
. " LEFT JOIN `daftarharga`.`tabelharga` ON `lokasiasal`.`id_asal` = `tabelharga`.`id_asal` \n"
. " LEFT JOIN `daftarharga`.`lokasitujuan` ON `tabelharga`.`id_tujuan` = `lokasitujuan`.`id_tujuan` \n"
. " WHERE tujuan = '$id_negara'";
$hasil = mysql_query($sql);
if (!$hasil)
print("Query tak dapat diproses"); /* <<<< this always came out every time i choose one of the combobox options */
else
{
$baris = mysql_fetch_row($hasil);
$data_ada = TRUE;
if (empty($baris))
{
print("Tak ada yang memenuhi!");
$data_ada = FALSE;
}
if ($data_ada)
{
// Creating Tables and column headers
print("<table border='1'>\n");
print("<tr>\n");
print("<td>Asal</td>\n");
print("<td>Tujuan</td>\n");
print("<td>Harga</td>\n");
print("</tr>\n");
}
while ($baris)
{
$asal = $baris[0];
$tujuan = $baris[1];
$harga = $baris[2];
// show to table
print("<tr>\n");
print("<td>$asal</td>\n");
print("<td>$tujuan</td>\n");
print("<td>$harga</td>\n");
print("</tr>\n");
// Read next data
$baris = mysql_fetch_row($hasil);
}
if ($data_ada)
print("</table>\n");
}
?>
pilihtujuan.php
<?php
header("Cache-Control: no-cache, must-revalidates");
header("Expires: Mon, 26 Jul 1997 00:00:00 GMT");
require_once("koneksi.php");
$benua = $_GET["benua"]; // Get URL Variable
$sql = "SELECT `lokasiasal`.`asal`, `lokasitujuan`.`tujuan`, `tabelharga`.`harga`\n"
. "FROM `lokasiasal`\n"
. " LEFT JOIN `daftarharga`.`tabelharga` ON `lokasiasal`.`id_asal` = `tabelharga`.`id_asal` \n"
. " LEFT JOIN `daftarharga`.`lokasitujuan` ON `tabelharga`.`id_tujuan` = `lokasitujuan`.`id_tujuan` \n"
. " WHERE asal = '$benua'";
$hasil = mysql_query($sql);
if (!$hasil)
print("Query tak dapat diproses");
else
{
$baris = mysql_fetch_row($hasil);
$data_ada = TRUE;
if (empty($baris))
{
print("Tak ada yang memenuhi!");
$data_ada = FALSE;
}
if ($data_ada)
{
// Create Options
print("<select id='id_negara' " .
"onchange='tampilkanInfoNegara()'>\n");
print("<option value=\"--\" selected=\"selected\">" .
"-- Pilih Tujuan --</option>");
}
while ($baris)
{
$asal = $baris[0];
$tujuan = $baris[1];
$harga = $baris[3];
// Show to options
print("<option value=\"$tujuan\">$tujuan</option>");
// Read next data
$baris = mysql_fetch_row($hasil);
}
if ($data_ada)
print("</table>\n");
}
?>
and the last one is connection (koneksi.php)
<?php
define("NAMA_SERVER", "localhost");
define("PEMAKAI", "*******");
define("PASSWORD", "******");
define("DATABASE", "******");
$id_mysql = mysql_connect(NAMA_SERVER, PEMAKAI, PASSWORD);
if (!$id_mysql)
die("Koneksi ke server gagal");
$db_daftarharga = mysql_select_db(DATABASE, $id_mysql);
if (!$db_daftarharga)
die("Koneksi ke server gagal");
?>