Loading...
 
Skip to main content

History: How To - using email address of current user

Source of version: 17 (current)

Copy to clipboard
             ## Need

I needed to use the email address of the current user to fill out a form field. (long story as to why)

## Explanation

There exist some ((Wiki Argument Variables|Wiki Argument Variables)) that allow one to use some variables that are part of the tiki current instance. For example {{user}} will display the userid of the current user: {{user}}. My first idea was to implement the same thing for {{email}} to display the email id of the current user.

It is generally considered a bad idea to insert email addresses into wiki or web pages as spammers can scrape and use them. I believe this is why the {{email}} idea is frowned upon. So thanks to Jonny's suggestion I came up with the following solution instead.

## Method

1. Add the following custom.php to your tiki language directory, *mine is in lang/en/ or lang/en-uk/*
    {CODE(colors=bash)}
<?php
// custom.php pmc 2011/05/11
global $user;
if (!empty($user)) {
  global $userlib, $smarty;
  $email=$userlib->get_user_email($user);
  //create smarty variable assign it the value of the current user's email address
  $smarty->assign('curremail', $email); }
{CODE}
2. To use the smarty variable 
    1. using ((PluginSmarty|Smarty plugin)) 
        {CODE(colors=bash)}
{SMARTY(name=>eval, var=>"{$curremail}")}{SMARTY}
{CODE}
    2. used in a smarty template 
        {CODE(colors=bash)}
email id is {$curremail}
{CODE}

## Example

This is how I used it via ((PluginSmarty|smarty plugin)) and ((PluginDiv|div plugin)) and ((PluginJQ|jquery plugin)) to pre-fill an html form field which is within ((PluginHtml|html plugin)). It is shown here in three parts that must be within the same page. Extraneous information has been removed &amp;/or replaced with *blah* or *...*.

- html &lt;form> 
    {CODE(colors=bash)}

{HTML()}

<form action="http://blahblah..." method="post">
Your E-mail address: <input id="emailid" name="email" size="30" type="text"></input>
... 
</form>
{HTML()} 
{CODE} - retrieve field value, assign it an id for use in jquery 
    {CODE(colors=bash)}
{DIV(class=hidden, id=emailactual)}
 {SMARTY(name=>eval, var=>"{$curremail}")}{SMARTY}
{DIV}
{CODE}
- insert the field value in the form 
    {CODE(colors=bash)}
{JQ()}
if (!$jq("#emailid").val()) { $jq("#emailid").val($("#emailactual").text()); 
}
{JQ}
{CODE}