Retrieving data from a form.

Discuss macro implementations, ask for macro help (to share your creations, see User Creations, probably either Campaign Frameworks or Drop-in Resources).

Moderators: dorpond, trevor, Azhrei, giliath, jay, Mr.Ice

Post Reply
zylosan
Giant
Posts: 102
Joined: Sun Jun 28, 2009 11:08 pm

Retrieving data from a form.

Post by zylosan »

Their have previously been a couple of good theads on the topic of retrieving data from a form via its action attribute. (I think thats correct). I want to create a input stats form with custom HTML formating and need to know how to recover the information from each field when ok is clicked.

User avatar
Rumble
Deity
Posts: 6235
Joined: Tue Jul 01, 2008 7:48 pm

Re: Retrieving data from a form.

Post by Rumble »

zylosan wrote:Their have previously been a couple of good theads on the topic of retrieving data from a form via its action attribute. (I think thats correct). I want to create a input stats form with custom HTML formating and need to know how to recover the information from each field when ok is clicked.


It depends on the method you use for the form. If you leave it blank (e.g., the method attribute of the form is blank) it defaults to sending a String Property List to the receiving macro, as the variable macro.args.

If you put method="json", it sends a JSON object where each key is the name of one of the input fields on the form, and the value of the form is passed along with.

So if you have the very basic form in a frame or dialog:

Code: Select all

<html>
<body>
<form name='myform' method='json' action='[r:macroLinkText("[email protected]:Whatever")]'>
Name: <input name='firstField' type='text'></input><br>
Rank: <input name='secondField' type='text'></input><br>
Is A PC: <input name='checkExample' type='checkbox'></input><br>
<input type='submit' name='button' value='Submit'></input>
</form>
</body>
</html>



When you click submit, the macro.args value in ProcessForm will be something like:

Code: Select all

"{'firstField':'a name here', 'secondField':'a rank here', 'checkExample':'on'}"


which you can then handle as a JSON object. Note one thing: for checkboxes, they only appear in the macro.args if they've been checked - if they aren't checked, they don't show up as 'checkExample':'off', they just don't show up at all.

zylosan
Giant
Posts: 102
Joined: Sun Jun 28, 2009 11:08 pm

Re: Retrieving data from a form.

Post by zylosan »

Questions
1. What if I want to call the form from inside a function and return the results inside the same function.
2. Is it possible to load the inputs inside the form with a value taken from a campagin property as you can with the input maptools macro.

Maptools Input Function
3. Is it possible to align more than one value on a line.

Line 1 Name Base Bonus Total
Line 2 Butt Kissing 10 11 22

Line 1 would just be a labal entery
Line 2 would be four sepearte input boxes.

zylosan
Giant
Posts: 102
Joined: Sun Jun 28, 2009 11:08 pm

Re: Retrieving data from a form.

Post by zylosan »

I am attempting to test the block of code rumble posted above. I can get the form to popup but the submit button does nothing, this is the code I am using.

Code: Select all

[ frame("Defense Roll"): {
<html>
<body>
<form name='myform' method='json' action='[r:macroLinkText("[email protected]:GURPS")]'>
Name: <input name='firstField' type='text'></input><br>
Rank: <input name='secondField' type='text'></input><br>
Is A PC: <input name='checkExample' type='checkbox'></input><br>
<input type='submit' name='button' value='Submit'></input>
</form>
</body>
</html>
}]


This is the InputStats macro it is suppost to call.

Code: Select all

[h: value = macro.args]

[h: text = json.get(value, "firstField")]

Text Output [text]

User avatar
Raoden
Dragon
Posts: 381
Joined: Fri Dec 18, 2009 2:33 am
Location: San Diego

Re: Retrieving data from a form.

Post by Raoden »

I think the problem is that [wfunc]macroLinkText[/wfunc] defaults to hiding its output from everybody. If you want the results of the InputStats macro to show up, you should change that line to this:

Code: Select all

<form name='myform' method='json' action='[r:macroLinkText("[email protected]:GURPS", "all")]'
"Fairy tales do not tell children the dragons exist. Children already know that dragons exist. Fairy tales tell children the dragons can be killed."
- G. K. Chesterton

Wonderful HTML/CSS reference * Color Manager * Token Manager 2.0

User avatar
biodude
Dragon
Posts: 444
Joined: Sun Jun 15, 2008 2:40 pm
Location: Montréal, QC

Re: Retrieving data from a form.

Post by biodude »

zylosan wrote:Questions
1. What if I want to call the form from inside a function and return the results inside the same function.

HTML forms don't work the same way as the [wfunc]input[/wfunc] function. The only way to interrupt macro execution with user input is via the [wfunc]input[/wfunc] function. If you want to use snazzy html form inputs in a frame or dialog, you'll have to re-organize your code, but it might not be by that much. If you search the forums for "auto-executing" frames / dialogs, you might find some good examples. Essentially, you can divide your macro into two sections: one to draw the input dialog/frame, and another to process the (json) form arguments when submitted (to itself). It requires a high-level [IF(), CODE:{}] block, but I find it a simple way to organize my code. The tricky part is if this is a function called by something else, in which case you'll need to completely change the flow of interaction between the user and your macro processes.
Drawing a dialog for input happens independently of user input: that happens when the user clicks a submit button. So macros keep executing after a dialog is drawn, and if further events require user input, you have to stop the macro, and set up "follow-up events" that happen after triggered by user input. If you need other data passed on after user input in a dialog, consider using <input type='hidden' ... /> to put in other values that are not displayed in the form, but will be included when the form is submitted (along with the user input).
If this makes no sense, I'm likely skipping over some details for the sake of brevity: feel free to ask for more info or clarification.

zylosan wrote:2. Is it possible to load the inputs inside the form with a value taken from a campagin property as you can with the input maptools macro.

Absolutely. This is a property of html forms (lots of resources online about this). Most HTML form elements allow you to specify a default value. Just include a variable reference in the HTML as you would for any other code-generated content. E.g.

Code: Select all

<input type='text' name='STR' value='[R: getProperty( "Strength" )]' /> 

Of course, if you are doing this a lot, I highly recommend doing it the [wfunc]strformat[/wfunc] way:

Code: Select all

[R: strformat( "<input type='text' name='STR' value='%{Strength}' />" )] <!-- note that this assumes 'Strength' is accessible as a variable 
or Property defined in the Campaign Properties
, unlike the previous example. --> 


zylosan wrote:Maptools Input Function
3. Is it possible to align more than one value on a line.

Line 1 Name Base Bonus Total
Line 2 Butt Kissing 10 11 22

Line 1 would just be a labal entery
Line 2 would be four sepearte input boxes.

Not ... really. It *might* be possible, in theory, given that you can include html (including form elements?) in a LABEL line of an [wfunc]input[/wfunc] dialog, HOWEVER, I have no idea if this would work as expected (given that they would be html form elements, not input fields).
Short answer: No. If you want richer formatting of input fields, you will have to use html forms in a frame or dialog.

This is the main trade-off between the input function and html forms: input is quicker and easier to 'insert' into your macro sequence of events, but very limited in formatting (that said, zEal has pushed the envelope on this one quite a lot, and there are a lot of places you can include html within input elements, you just can't position them with html, as far as I know).
"The trouble with communicating is believing you have achieved it"
[ d20 StatBlock Importer ] [ Batch Edit Macros ] [ Canned Speech UI ] [ Lib: Math ]

User avatar
Rumble
Deity
Posts: 6235
Joined: Tue Jul 01, 2008 7:48 pm

Re: Retrieving data from a form.

Post by Rumble »

zylosan wrote:Questions
1. What if I want to call the form from inside a function and return the results inside the same function.


Use input(), or split it into 3 macros:

Call form at end of first half of function.
Collect data with form.
Send form to second half of function.

2. Is it possible to load the inputs inside the form with a value taken from a campagin property as you can with the input maptools macro.


Yes, you would handle that like any HTML form - you can set the value of form fields.


Maptools Input Function
3. Is it possible to align more than one value on a line.

Line 1 Name Base Bonus Total
Line 2 Butt Kissing 10 11 22

Line 1 would just be a labal entery
Line 2 would be four sepearte input boxes.


Not to the best of my knowledge, no.

prestidigitator
Dragon
Posts: 317
Joined: Fri Apr 23, 2010 8:17 pm

Re: Retrieving data from a form.

Post by prestidigitator »

Rumble wrote:It depends on the method you use for the form. If you leave it blank (e.g., the method attribute of the form is blank) it defaults to sending a String Property List to the receiving macro, as the variable macro.args.

If you put method="json", it sends a JSON object where each key is the name of one of the input fields on the form, and the value of the form is passed along with.

Really?! Oh, kick DONKEY! I assume that means I can stop worrying my head off over semicolon injection in text fields? Woo hoo! 8)

(Donkey. Right. :lol:)
"He knows not how to know who knows not also how to un-know." --Sir Richard Burton

Post Reply

Return to “Macros”