I'm currently having an issue with getting javascript and python to communicate in web2py.
controller:
def testDB():
a=[]
a.append('a')
a.append('b')
a.append('c')
return dict(m=a)
(Eventually this will be replaced with an array of DB rows) Currently I'm trying to assign the values in m to an array in javascript. I've tried this a few ways:
var t="{{=m}}";
Returns about 43 meaningless characters. Then I tried:
var t= new Array(); var i=0;"{{q=0}}"
"{{i=len(message)}}"
i="{{=i}}";
for(q=0;q<i;q++,"{{q=q+1}}"){
t[q]="{{m[q]}}";
}
Which failed because the python q variable would reset every time the loop did, which is the heart of my problem. I also tried using pop:
for(q=0;q<i;q++,"{{q=q+1}}"){
alert("{{m.pop()}}");
}
But the array keeps resetting again at the start of the loop, so it keeps showing the same variable. Is there a simpler way to copy the arrays, or to stop the python variables resetting, or even simply to plug the javascript q variable into "{{m[q]}}" instead?
From what I've found, the problem comes from python being server side so you can't assign javascript values to it's variables, but I'm not sure what that has to do with the loop part of it (If I do the same outside a loop, the values do not reset). The common solution seems to be to use ajax or json, but I'd like to avoid that if possible. Thanks