I'm trying to get a variable but it's returning undefined. Maybe I'm having an issue with scope or timing?
var pid = getPart(upc);
console.log('pid: '+pid);
addPart(pid);
getPart() is not returning a value but the post is working and getting id. I just can't log pid or use it in addPart() because it seems to not be returning.
function getPart(upc){
$.post('include/_getpart.php', {upc: upc}, function(data){
name = data.name;
id = data.id;
return id;
}, "json");
}
For completeness, I will post my entire script.
<script language="javascript">
var success = 0;
var addedArray = [];
function add(form) {
console.log('form for '+form.name);
$(':input[type="number"]', form).each(function() {
if (this.value > 0){
upc = this.name;
qty = this.value;
console.log(upc + ': ' + qty);
var pid = getPart(upc);
console.log('pid: '+pid);
//addPart(pid);
}
});
if (success==1) {
console.log(addedArray);
}
}
function getPart(upc){
$.post('include/_getpart.php', {upc: upc}, function(data){
name = data.name;
id = data.id;
/*
console.log('name: '+name);
console.log('product id: '+pid);
var productArray = new Array(name, qty);
console.log(productArray);
addedArray.push(productArray);
*/
return id;
}, "json");
}
function addPart(pid){
if (!pid) {
console.log('error: product id (pid) was not found');
return;
} else {
$.post('include/_addpart.php', {pid: pid, qty: qty}, function(data) {
console.log('_addpart.php response: '+data);
});
}
}
</script>
All the ajax requests work, I'm wondering if the bit of lag the may make while communicating with the server warrants the setting of said variable to be skipped. If so, should I put a timeout in there, and how.