Buy Now
Sign in

FAQ search

Variable shortcodes

Details
Published: Friday, 26 May 2017 14:40

Both Chronoforms v6 and ChronoConnectivity v6 have a new powerful feature, its the variable shortcodes, for example, you can now capture request data and pass it to any element in your form, including the form fields interface or the email or any database action, there are so many shortcodes supported and knowing them can simplify many tasks which required writing php code in previous versions.

in this article, some of the supported shortcodes are demonstrated, sometimes with example usage:

Request data

Use {data:field_name} or {data:url_parameter_name} any where you want to use the value of a request parameter, for example you can include a field value in an email, a message to the user or in a PDF file, you may also use any shortcode in any field settings, like a field label or value.

Using {data:} only will return the full request data array, this may be used, for example, when saving the form data to the database, the "Save data" action requires a dataset to use it as the storage data source.

Please note that the shortcodes support accessing nested arrays, so using {data:array.field} will get the value of the "field" key under the "array" key data in the request data.

Some shortcodes have some methods to apply to the data before returning the result here are some examples:

  1. Empty: return true or false, based on the variable value: {data.empty:param}
  2. Count: return the count of an array, example: {data.count:checkboxes}
  3. Print: print a variable in a readable format, example: {data.pr:}
  4. Break: Convert new line characters to line breaks, useful for getting a formatted output of textareas. {data.br:textarea}
  5. Json encode: encode the value in json format: {data.jsonen:checkboxes}
  6. Json decode: Decode a json encoded value: {data.jsonde:stored_value}
  7. Join: Join an array of values into 1 string: {data.join:values} or {data.join[,]:values}
  8. Split: Split a joined string into an array of values: {data.split:string}
  9. Clear: clear the variable value and set it to null: {data.clear:not_needed}

You can also set a default value to be used in case the called variable value is null or undefined: {data:non_existent/1}, this call will return 1 if the variable is not defined or not available in the request data.

You can not use the shortcode directly in a PHP action, use this function instead: $this->data("field_name", "default value");

Functions/Actions data

Almost every processed function (action) returns a result variable, the variable type is different, some will return a boolean true or false, others will return an array of values, this variable can be accessed using this syntax: {var:function_name}

The function_name is visible inside a black label inside the function/action body and inside the "Name" setting box (in Connectivity).

So for example, the "Read Data" function returned result(s) are available under this variable: {var:read_data1}, assuming the function name is read_data1

The variable value is available ONLY AFTER the function has been processed.
In PHP you can use the following code to set and get variables: $this->set("var_name", "value"); and $this->get("var_name", "default");

Session data

Get a session variable using {session:variable_name} and set it using {session.set:var_name$value}

URLs

Get the current page url using {url:_self}, get a form event url using {url:even_name}, for example: {url:submit}, you can also append parameters to the url: {url:submit$param=value}

Redirect

Redirect to any url: {redirect:url_here}, or to a form event: {redirect:event_name}

Use variables inside the redirect shortcode: {redirect:http://domain/form$param=(data:some_var)}

User data

Get the user session data using the {user:} shortcode, example: {user:id}, {user:username} or {user:email}

Date and Time

Get the current date in Y-m-d H:i:s format: {date:}, or pass the desired format: {date:H:i}.

Or pass a date value to be formatted: {date:Y-m-d$(var:some_date_value_or_timestamp)}

Page information

Get information about the current page: {page:url} or {page:title}

Locales

Get a locales string value: {l:string} or {lang:string}

Other useful shortcodes:

  1. {rand:} to get a random number.
  2. {uuid:} to get a UUID string.
  3. {ip:} to get the user ip.