Forum

Posted by
Matthew Hardy  -  September 2008
A simple tag that helps access form variables.
Needs more query validation - any tips on that?.

(I notice that my br html was not previewing. substituted [] for brackets...)
------8<----------8<------

string simpletag_basicthrash(string tag_name, mapping args, string contents, RequestID id )
{


string basicurl = "";
string newurl = "";
mapping url;

if(sizeof(id->data))
basicurl = id->data;

else
{
        if(id->query)
        basicurl = id->query;

        else
        return "No Query Yet";
}

//FIXME validation needed
url = mkmapping(((basicurl / "&")[*]/"=")[*][0], ((basicurl / "&")[*]/"=")[*][1]);;

foreach(url;string x; string y)
{
        newurl += x + "=" + y + "[br/]";
}
        
return sprintf("%O", newurl);
}

 
Posted by
Erik Dahl  -  September 2008
I took the opportunity to use the more modern tag API in Roxen and I hope I understands your problem, so I figure that i should kind of give it to you how I would do it. Although I do believe I could accomplish the same thing pretty easy in RXML, but I don't know the context you are needing this tag... Anyhow my modification quotes the resulting string using Roxen.roxen_encode(string value, string encoding_type)
Depending on your version of Roxen, there are some in the example that do not have some of the encodings and probably are some of the encodings in there totally irrelevant to your usage:

// [basic-trash]
// [basic-trash quote="js"/]
// [basic-trash quote="utf8"]
// [basic-trash quote="base-64"] - Not that usefull
// etc...
class TagBasicThrash
{
  inherit RXML.Tag;
  constant name = "basic-thrash";

  mapping(string:RXML.Type) opt_arg_types = (["quote": RXML.t_text(RXML.PEnt)]);

  class Frame {
    inherit RXML.Frame;
    array do_enter(RequestID id) {
      array(string) form_values = ({});
      if(id->variables && sizeof(id->variables)) {
foreach(indices(id->variables), string var_name) {
  string|array(string) value = id->variables[var_name];
  if(arrayp(value))
    value = value * "";
  form_values += ({
    Roxen.roxen_encode(var_name, args->quote||"url") +
    "=" +
    Roxen.roxen_encode(var_name, args->quote||"url")
    });
}
      }
      return ({ form_values * "" });
    }
  }
}


In RXML (and sorry it has to be in one line and replaced with brackets and dollar sign = ampersand):
[emit source="values" from-scope="form"][if variable="_.counter ] 1"]$$_.index;=$_.value:url;[/if][else]$_.index:url;=$_.value:url;[/else][/emit]
 
Posted by
Matthew Hardy  -  October 2008
thx.

What is the best variable to use when looking for a post from a form?
id->data seems to have what I need but extracting the index:value is not as easy as I initially thought.

 
Posted by
Tobias Liin  -  October 2008
What is the best variable to use when looking for a post from a form?
id->data seems to have what I need but extracting the index:value is not as easy as I initially thought.



I'm not sure this is what you are asking for, but anyways:

If you have a form with a text field named "fname", you could extract this in Pike by doing:

id->variables->fname.

Take a look in any of the editor modules in modules/sitebuilder/modules/editor/ for an example.
 
Posted by
Matthew Hardy  -  October 2008
id->variables are good and work - I note that the indices(id->variables) are listed as FakedVariables. I think a user can add to this list quite easily by completing a URL.

In id->data I noticed that the variables are collected from a form post, which I presume makes it a little harder to copy paste a URL.

Main thing I want is to be clear in the Pike module what variables I want to process + prevent the caching of URL (long addresses) in the browser history.

but id->data seems to provide too much Webkit information from safari, and my indices() & values() only provide raw number values.

I guess a concise  description of all the Roxen variables available and how/why to use them would be great for Webserver development. ie. id->query is nice for finding a URL variable set.
id->variables->formthingy is perfect for accessing that value etc. 

I have left it with using foreach(indices(id->variables) string y){...} for now and then strip out known variable names that I don't want processed ie. a cookie that floats around in id->variables, but did not come from the form etc.

cheers!

 
Posted by
Martin Stjernholm  -  October 2008
Most things in the id object have decent documentation in base-server/prototypes.pike. (A working extractor for the pike refdoc inside WebServer etc is on the wishlist.)

There you can for instance see that id->real_variables is the actual source for the variables, and id->variables is only a compatibility wrapper for it.
 
Posted by
Matthew Hardy  -  October 2008
tnx
 
1
Search this thread: