JSON- PHP generated JS-file question
So i'm back again. The idea is still to type in a code, and automatically show the price of that item. I'm getting close and i have the following:
-
<script language="javascript" type="text/javascript">
$(function() {
$("input").next("#prijs").hide();
$("#naam").blur(function () {
$.getJSON("producten.js", function(json) {
/* Parse JSON objects */
jJSON["prijs"] = (function() {
response = {
values:[],
};
$.each(json.channel.items,function(i,item) {
if(item.naam == $("#naam").val()){
if (item.prijs != "undefined") {
response.values = item.prijs;
$("#prijs").text(item.prijs);
}
}else{alert("nope code does not exist");}
});
return response;
})();
$("#prijs").show();
});
});
});
var jJSON = {
getValues: function(obj) {
return jJSON[obj]["values"];
},
};
</script>
HTML
-
<form action="" method="" accept-charset="utf-8">
<p><input type="text" id="naam" /> <span id="prijs"></span></p>
<p><input type="submit" value="Continue" /></p>
</form>
<p><span id="totaal"></span></p>
I generate the producten.js file with PHP each time the DB will be adjusted. It contains :
-
{"channel":{"items":[{"id":"1","prijs":"10","naam":"a"}, {"id":"2","prijs":"5","naam":"b"}, {"id":"3","prijs":"22","naam":"c"}]}}
now the problem was that i did not know how to communicate with a JS file. Therefore i used google and came op with:
-
/* Parse JSON objects */
jJSON["prijs"] = (function() {
response = {
values:[],
};
and
-
var jJSON = {
getValues: function(obj) {
return jJSON[obj]["values"];
},
};
it's a code i slightly modified from a page i found. But it's also the reason of me posting this. it makes a values[] array and loads ALL the prices of ALL the products, this is not what i want to achief. I just want 1 price, that one that i ask for in the inputfield.
so now the alert(nope code does...) always goes off, which i understand. But i think there must be a way to achief what i want without the extra function, i just can't seem to figure out how ? anyone ?
(if i find the sollution i wil post it here for others to enjoy)