Skip to content

Instantly share code, notes, and snippets.

@ear1grey
Forked from anonymous/gist:5340810
Last active December 15, 2015 23:49
Show Gist options
  • Save ear1grey/5343332 to your computer and use it in GitHub Desktop.
Save ear1grey/5343332 to your computer and use it in GitHub Desktop.
This is a slight restructuring which removes one of the HTTP calls (the null one) and moves the callback code within the addItem function, forming a closure.
function createObject() {
var request_type;
var browser = navigator.appName;
if(browser == "Microsoft Internet Explorer"){
request_type = new ActiveXObject("Microsoft.XMLHTTP");
}
else{
request_type = new XMLHttpRequest();
}
return request_type;
}
// var httpAddItem = createObject();
// httpAddItem.open('get', 'nullPage.php');
// httpAddItem.send(null);
function addItem() {
var httpAddItem = createObject();
console.log(httpAddItem.readyState);
var itemName = encodeURI(document.getElementById('itemNameInput').value);
var itemDescription = encodeURI(document.getElementById('itemDescriptionInput').value);
var itemPrice = encodeURI(document.getElementById('itemPriceInput').value);
var itemQuantity = encodeURI(document.getElementById('itemQuantityInput').value);
var itemImage = encodeURI(document.getElementById('fileInput').value);
nocache = Math.random();
httpAddItem.onreadystatechange = function () {
//alert("yo");
//alert(httpAddItem.readyState);
//alert(httpAddItem.responseText);
window.console.log(httpAddItem.readyState);
if(httpAddItem.readyState == 4){
// else if login is ok show a message: "Site added+ site URL".
var addNewItem= confirm("Do you want to add another item?");
alert("yo");
if (addNewItem== true){
alert("hi");
}else{
alert("Meany!!!");
}
}
else{
console.log("Loading");
}
window.console.log(httpAddItem.readyState);
}
httpAddItem.open('get', 'addItem.php?itemName='+itemName+'&itemDescription='+itemDescription+'&itemImage='+itemImage+'&itemPrice='+itemPrice+'&itemQuantity='+itemQuantity+'&nocache='+nocache, true);
httpAddItem.send(null);
//responseText = deleteItemhttp.responseText;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment