JSON Sample question

Topics: User Forum
Coordinator
Jul 10, 2013 at 7:41 PM
Edited Jul 10, 2013 at 7:45 PM
Here's my HTML:
<form name="theform">
<div><input type="text" name="a" value="1" id="a" /></div>
<div><input type="text" name="b" value="2" id="b" /></div>
<div><input type="hidden" name="c" value="3" id="c" /></div>
<div>
<textarea name="d" rows="8" cols="40">4</textarea>
</div>
<div><select name="e">
<option value="5" selected="selected">5</option>
<option value="6">6</option>
<option value="7">7</option>
</select></div>
<div>
<input type="checkbox" name="f" value="8" id="f" />
</div>
<div>
<input type="submit" name="g" value="Submit" id="g" onclick='submitform()' />
</div>
</form>
If I want to pass a form using the following script, I can do this right?:
function submitform(){
var formjson = $('#theform').serializearray();
var myvfpurl = "<%=oProp.ScriptPath+[?action=vfpTestJson]%>";

$.ajax({
url:'myvfpurl',
type:'POST';
data:{thehtmlform: formjson},
})
}
Then on the aVFP backend, how do I get each fields and it's corresponding values?
if oProp.Action=="vfpTestJson"
m.lsval = NVL(oRequest.Form("thehtmlform"),[])
endif

Can the built-in jSON parser work on achieving what I wanted?

Thank you.
Coordinator
Jul 10, 2013 at 8:25 PM
Have you tried the Parse method yet in the JSON class (http://activevfp.codeplex.com/SourceControl/latest#avfp6.03_source/prg/json.prg)?? Play around with that - it's the standard way to extract raw JSON which is being passed back up to the server.
Coordinator
Jul 10, 2013 at 9:33 PM
Edited Jul 11, 2013 at 2:34 PM
I actually did and a little bit confused based on the actual output and the output that I wanted.

Basically if I use this:
oJSON=NEWOBJECT('json')
m.lcMultiArrayJSON = oJSON.stringify(oRequest.Form("thehtmlform"))

It will give me a string value. The same result of output if I added this:
m.NewVal = m.loJSON.Parse(m.lcMultiArrayJSON)

In my actal page the string value will be:
a=1&b=2c=3....

I was expecting the parsing to be more of an array like which it can be accessed easily like:

thisisthevaluefortheTextAField = m.NewVal.a
thisisthevaluefortheTextBField = m.NewVal.b

I think I'm missing something.
Coordinator
Jul 10, 2013 at 9:37 PM
Check out the original Craig Boyd class and description at:

http://www.sweetpotatosoftware.com/spsblog/CommentView,guid,5ac8d202-98b4-4ee8-998d-692e4b597bec.aspx

I had to make some adjustments to get stringify to work the way I needed it and you may have to do the same for Parse.
Coordinator
Jul 11, 2013 at 3:19 PM
Edited Jul 11, 2013 at 3:20 PM
Just to set some points here before I investigate the json parser.

If you use this:
var formjson = $('#theform').serializearray();
then you are actually creating a variable array on the javascript side;

If you use this:
var formjson = $('#theform').serialize();
then you are creating a variable of string.

My next question will be, how do you accept an array on the server side? If I do this using the serializearray function I get null.
m.lsval = NVL(oRequest.Form("thehtmlform"),[])

on the other hand, if I use serialize(), then I get a string value.
" a=1&b=2c=3...."
Coordinator
Jul 11, 2013 at 3:30 PM
You might want to take a look at this thread:

http://activevfp.codeplex.com/discussions/276999

It's dealing with XML but might be a similar issue. In AVFP we are subclassing the REQUEST object which makes it easier to use but a little less obvious as far as how to get everything coming across the wire - you have to use oRequest.oRequest to get some of the values being passed over such as:

xmlDoc.load(oRequest.oRequest)
Coordinator
Jul 18, 2013 at 2:47 PM
Edited Jul 18, 2013 at 2:48 PM
I have found the obvious since I was using version aVFP 5.XX. The jSON implementation included in this version that I have seems buggy. When I added the latest version from Craig's into my project, it worked like a charm. I will try to post some sample code on this thread once I completed my work.

Btw, Claude can you put a version number on the activevfp.dll file on the next release, this I think will be helpful on similar case that I have where I have no easy way of knowing what version I'm running under my project folder.

Thank you. //apa
Coordinator
Jul 18, 2013 at 6:41 PM
"Btw, Claude can you put a version number on the activevfp.dll file on the next release, this I think will be helpful on similar case that I have where I have no easy way of knowing what version I'm running under my project folder."

Ok, added to Item list for next release.