Commit 87ea5e1d authored by Clara Stoddart's avatar Clara Stoddart

Writing ajax request

parent 52aa5267
hello this is ajax
// Unique ID for the className.
var MOUSE_VISITED_CLASSNAME = 'crx_mouse_visited';
var MOUSE_VISITED_CLASSNAME = 'crx_mouse_visited_true';
var MOUSE_VISITED_CLASSNAME_FALSE = 'crx_mouse_visited_false';
var srcName;
var dietNum = 0;
var requestResult;
// var currName;
//Querying database on heroku
......@@ -16,6 +18,49 @@ var dietNum = 0;
// }
// });
//https://www.w3schools.com/nodejs/nodejs_mysql_select.asp
function callBack(canEat) {
if (canEat == undefined) {
requestResult = true;
} else if (canEat == "0") {
requestResult = true;
} else if (canEat == "1") {
requestResult = true;
} else if (canEat == "Could not connect") {
result = true;
} else if (canEat == 1) {
requestResult = true;
} else if (canEat == '1') {
requestResult = true;
}
}
function checkAllergen(product) {
request(product);
return requestResult;
}
function checkAllergen(product) {
var xhr = new XMLHttpRequest();
var result;
xhr.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
var canEat = this.responseText;
if (canEat == undefined) {
result = false;
} else if (canEat == "0") {
result = false;
} else if (canEat == "1") {
result = true;
} else if (canEat == "Could not connect") {
result = false;
}
}
};
xhr.open("GET", "script.php?q="+product, true);
xhr.send();
return result;
}
// Previous dom, that we want to track, so we can remove the previous styling.
......@@ -40,7 +85,12 @@ document.addEventListener('mousemove', function (e) {
// srcElement.classList.add(MOUSE_VISITED_CLASSNAME);
// }
// currName = srcElement.srcName;
if (checkAllergen(srcName)) {
srcElement.classList.add(MOUSE_VISITED_CLASSNAME); //changes outline of pic
}
if (!checkAllergen(srcName)) {
srcElement.classList.add(MOUSE_VISITED_CLASSNAME_FALSE);
}
//TODO: modify behaviour according to different allergens
// The current element is now the previous. So we can remove the class
// during the next ieration.
......@@ -82,4 +132,3 @@ function myFunction() {
// return false;
// });
// }
......@@ -3,7 +3,7 @@ $product = $_GET['q'];
$con = mysqli_connect('localhost','root','','public');
if (!$con) {
echo "Could not connect";
die('Could not connect: ' . mysqli_error($con));
//die('Could not connect: ' . mysqli_error($con));
}
// $sql = "SELECT containsgluten FROM productsingredients WHERE product = $product";
$sql = "SELECT containsgluten FROM productsingredients WHERE product = Waitrose Delicious & Zingy Lemon Loaf Cake";
......
......@@ -33,4 +33,6 @@
}
......@@ -11,7 +11,12 @@
background-color: #ffffff;
}
.crx_mouse_visited {
.crx_mouse_visited_true {
background-clip: #bcd5eb!important;
outline: 1px dashed #47bb47!important;
}
.crx_mouse_visited_false {
background-clip: #bcd5eb!important;
outline: 1px dashed #fa1100!important;
}
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment