//php
//dbrequest.php
mysql_connect('localhost', 'root', 'passwort');
mysql_select_db('wemepes_ch');
if( isset($_GET['data']) ){
$sql = 'SELECT * FROM `bookmark` WHERE `vertrag` = "'.mysql_real_escape_string($_GET['data']).'"';
$result = mysql_query($sql);
$row = mysql_fetch_assoc($result);
echo $row['vertrag'];
}
else{
echo 'Invalid Request';
}
//php
//html
<!--- Die Datei die im Browser angezeigt wird -->
<html>
<head>
<!-- Javascript Datei einbinden -->
<script type="text/javascript" src="main.js"></script>
<title>Mein Ajax Test</title>
</head>
<body>
<div id="output"></div>
<select onchange="sendRequest('dbrequest.php?data='+this.value, 'output');">
<option value="1">1. Option</option>
<option value="2">2. Option</option>
</select>
</body>
</html>
//html
---------------------------------------------------------
//php
//main.js
function sendRequest(url, gcname){
//erstellen des requests
var req = null;
try{
req = new XMLHttpRequest();
}
catch (ms){
try{
req = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (nonms){
try{
req = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (failed){
req = null;
}
}
}
//anfrage erstellen (GET, url ist localhost,
//request ist asynchron
if (req == null)
alert("Error creating request object!");
req.open("GET", url, true);
//Beim abschliessen des request wird diese Funktion ausgeführt
req.onreadystatechange = function(){
switch(req.readyState) {
case 4:
if(req.status!=200) {
alert("Fehler:"+req.status);
}else{
alert(req.responseText);
//schreibe die antwort in den div container mit der id content
document.getElementById(id).innerHTML = req.responseText;
}
break;
default:
return false;
break;
}
};
req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
req.send(null);
}
//php
//-------------------------------------------------------