How can i make make a variable in a javascript and give it a value from an json array?
The array is sent from a php script and looks like this:
php
$stat = array("v1" => "$v1", "v2" => "$v2", "v3" => "$v3",
"v4" => "$v4", "v5" => "$v5", "pump" => "$pump", "flow" => "$flow");
echo json_encode(($stat));
html/javascript
$.ajaxSetup({ cache: false });
setInterval(function(){
$.getJSON('statusdata.php',function(data) {
$.each(data, function(key, val) {
// I try to do something like this..
var v1 = key[1];
var v2 = key[2];
and so on..
Then i want to use a variable to alert a popup-window with some kind of warning.
Someting like:
if (v1 == 1){
run the popup function!
}
Can anyone help me?
$.each
works: api.jquery.com/jQuery.each. FYI,data
will be an object, sokey
will be the name of the property (e.g.'v1'
,'pump'
) andval
will be the value of that property. – Felix Kling Apr 7 '13 at 17:45