Retrieving data from a form.
Moderators: dorpond, trevor, Azhrei, giliath, jay, Mr.Ice
Retrieving data from a form.
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.
Re: Retrieving data from a form.
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.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.
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("ProcessForm@Lib: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'}"
Re: Retrieving data from a form.
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.
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.
Re: Retrieving data from a form.
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.
This is the InputStats macro it is suppost to call.
Code: Select all
[ frame("Defense Roll"): {
<html>
<body>
<form name='myform' method='json' action='[r:macroLinkText("InputStats@Lib: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>
}]
Code: Select all
[h: value = macro.args]
[h: text = json.get(value, "firstField")]
Text Output [text]
Re: Retrieving data from a form.
I think the problem is that Wiki: macroLinkText() 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("InputStats@Lib: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
- G. K. Chesterton
Wonderful HTML/CSS reference * Color Manager * Token Manager 2.0
Re: Retrieving data from a form.
HTML forms don't work the same way as the Wiki: input() function. The only way to interrupt macro execution with user input is via the Wiki: input() 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.zylosan wrote:Questions
1. What if I want to call the form from inside a function and return the results inside the same function.
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.
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.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.
Code: Select all
<input type='text' name='STR' value='[R: getProperty( "Strength" )]' />
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. -->
Not ... really. It *might* be possible, in theory, given that you can include html (including form elements?) in a LABEL line of an Wiki: input() dialog, HOWEVER, I have no idea if this would work as expected (given that they would be html form elements, not input fields).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.
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 ]
[ d20 StatBlock Importer ] [ Batch Edit Macros ] [ Canned Speech UI ] [ Lib: Math ]
Re: Retrieving data from a form.
Use input(), or split it into 3 macros:zylosan wrote:Questions
1. What if I want to call the form from inside a function and return the results inside the same function.
Call form at end of first half of function.
Collect data with form.
Send form to second half of function.
Yes, you would handle that like any HTML form - you can set the value of form fields.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.
Not to the best of my knowledge, no.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.
-
- Dragon
- Posts: 317
- Joined: Fri Apr 23, 2010 8:17 pm
Re: Retrieving data from a form.
Really?! Oh, kick DONKEY! I assume that means I can stop worrying my head off over semicolon injection in text fields? Woo hoo!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.
(Donkey. Right. )
"He knows not how to know who knows not also how to un-know." --Sir Richard Burton