Hvad gør jeg forkert ?
Her følgene koder.java.php
<body>
<script language="javascript" type="text/javascript">
<!--
//Browser Support Code
function ajaxFunction(){
var ajaxRequest; // The variable that makes Ajax possible!
try{
// Opera 8.0+, Firefox, Safari
ajaxRequest = new XMLHttpRequest();
} catch (e){
// Internet Explorer Browsers
try{
ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try{
ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e){
// Something went wrong
alert("Your browser broke!");
return false;
}
}
}
// Create a function that will receive data sent from the server
ajaxRequest.onreadystatechange = function(){
if(ajaxRequest.readyState == 4){
var ajaxDisplay = document.getElementById('brand');
ajaxDisplay.innerHTML = ajaxRequest.responseText;
}
}
var stel = document.getElementById('stel').value;
var brand = document.getElementById('brand').value;
var queryString = "?stel=" + stel + "&brand=" + brand;
ajaxRequest.open("GET", "ajax-example.php" + queryString, true);
ajaxRequest.send(null);
}
//-->
</script>
<form name='myForm'>
stel: <input type='text' name="stel" id='stel' value="VF3" /> <br />
mærke: <input type='text' name="brand" id='brand' />
<input type='button' onclick='ajaxFunction()' value='Query MySQL' />
</form>
</body>
</html>
ajax-example.php
$dbuser = '';
$dbpass = '';
$dbname = '';
$dbhost = "localhost";
//Connect to MySQL Server
mysql_connect($dbhost, $dbuser, $dbpass);
//Select Database
mysql_select_db($dbname) or die(mysql_error());
// Retrieve data from Query String
$get_brand = $_GET['stel'];
// Escape User Input to help prevent SQL Injection
$stel = mysql_real_escape_string($stel);
//build query
$query = "SELECT * FROM vin_brand WHERE vin = '$get_brand'";
//Execute query
$qry_result = mysql_query($query) or die(mysql_error());
// Insert a new row in the table for each person returned
while($row = mysql_fetch_array($qry_result)){
$brand = $row[brand];
}
echo $brand;
?>
Felt "stel", skulle søges efter i db. Og resultat skulle blive udskrivet i felt "brand"'