Hi,
My codes:
chuffbucket.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script src="localhost/godfrey/Chuff Bucket/foods.js"></script>
</head>
<body>
<h1>The Chuff Bucket</h1>
<form>
Enter food item: <input type="text" id="userinput" name="userinput" onkeyup="processit(this.value)" />
</form>
<p id="resptext">Response here...</p>
</body>
</html>
foods.js
function processit(str){
if (str.length==0){ document.getElementById("resptext").innerHTML="Response here...";
return;
}
var xmlhttp = createXmlHttpRequestObject();
function createXmlHttpRequestObject(){
var xmlhttp;
// Check if using non-IE browsers
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
return xmlhttp;
}
if (xmlhttp) {
try {
// Open function configures connection to server settings
xmlhttp.open("GET","http://localhost/godfrey/Chuff Bucket/foods.php?str="+str,true);
xmlhttp.onreadystatechange=function(){
if (xmlhttp.readyState==4 && xmlhttp.status==200){
document.getElementById("resptext").innerHTML=xmlhttp.responseText;
}
xmlhttp.send(null);
}
} catch(e) {
alert( e.toString() );
}
}
//alert(str);
}
foods.php
<?php
$q = $_GET["str"];
$foods = array("tuna","ham","fish");
if (isset($q)){
if (!empty($q)){
if (in_array($q,$foods)){
echo ("We have ". $q);
} else {
echo ("We don't have such food");
}
}
}
?>
I cant seem to get the javascript (foods.js) to communicate with the php file (foods.php).
All files are in the same directory.