Welcome to the Learning Center

The Guide | Knowledge Base | FAQ

Browse it all or refine your selection using the filters below on the left.

Saving User Information in Your Form

How do you store user information like a user's ID or name with your data? Read this article and discover the answer.

By: Kelly Ford On: 05/16/2014

Link to this Article
https://dnndev.com/Learn/Guide/Article/saving-user-information-in-your-form

XMod Pro provides you with a lot of information about logged in users like their ID and name. However, a stumbling point for many newcomers is how do they put that information to use? Usually this means "How do I store a User ID (or other user info) with a record?"  We're going to tackle that question in this article.

There are really two ways you can approach saving user data. Storing the data in a hidden field and using XMod Pro's <Variable> tag. Which you use depends on your needs but we'll cover both.

The Hidden Field Approach to Saving User Data

This method involves SELECTing the user information as if you were retrieving it from the database and then letting XMod Pro handle binding it in an invisible control on the form. Let's start with a basic form and then modify it. Let's assume we have a News Article form. Forget about validation and layout and styling and even the article body. We just want to focus on the mechanics.

<AddForm>
  <SubmitCommand CommandText="INSERT INTO Articles(Title) VALUES(@Title)" />
  <Label>Title</Label>
  <TextBox Id="Title" DataField="Title" />
</AddForm>

This is straightforward. We're just storing a Title in the Articles table. Now, your boss comes to you and asks "How do we know who wrote the article?". You, being the brilliant one, immediately say "We'll store the user's ID along with the Article."  How would you modify the form to do this?

First, change the SubmitCommand to make use of the user's ID.

<AddForm>
  <SubmitCommand CommandText="INSERT INTO Articles(Title, UserID) VALUES(@Title, @UserID)" />
  <Label>Title</Label>
  <TextBox Id="Title" DataField="Title" />
</AddForm>

The next step is to SELECT the User's ID from the XMP User token. We do that by adding a <SelectCommand> tag to the form.

<AddForm>
  <SelectCommand CommandText="SELECT @uid AS UserID">
    <Parameter Name="uid" Value='[[User:ID]]' DataType="Int32" />
  </SelectCommand>
  <SubmitCommand CommandText="INSERT INTO Articles(Title, UserID) VALUES(@Title, @UserID)" />
  <Label>Title</Label>
  <TextBox Id="Title" DataField="Title" />
</AddForm>

The SelectCommand's CommandText looks suspiciously like a SQL SELECT command. What we're doing is taking value of the [[User:ID]] token and assigning it to the @uid parameter using the  <Parameter> tag). The SELECT command, then, is assigning that value to a field named UserID.  The last thing to do is add a control to our form that will store that information. We'll use a TextBox for that purpose.

<AddForm>
  <SelectCommand CommandText="SELECT @uid AS UserID">
  <Parameter Name="uid" Value='[[User:ID]]' DataType="Int32" />
  </SelectCommand>
  <SubmitCommand CommandText="INSERT INTO Articles(Title, UserID) VALUES(@Title, @UserID)" />
  <Label>Title</Label>
  <TextBox Id="Title" DataField="Title" />
  <TextBox Id="UserID" DataField="UserID" DataType="Int32" Style="display:none;" />
</AddForm>

In this version we've added another TextBox and assigned it the DataField of UserID. This binds it to the Field we defined in our SelectCommand. You'll also notice that we didn't add a <Label> tag. That's because we don't intend for this control to be displayed to the end user.  In fact, we added a Style property to the control and set its display to none, to hide it. 

Since the control has its display set to none, the user won't see it on her form, but it will still be visible in the HTML source code. So, don't store sensitive data in this manner. In our case, though, this approach works just fine.

The <Variable> Approach to Saving User Data

XMod Pro forms offer the <Variable> tag to save us a lot of the typing we had to go through in the Hidden Field approach.  In fact, it's so simple, I'll just show the finished code:

<AddForm>
  <Variable Name="UserID" Value='[[User:ID]]' DataType="Int32" />
  <SubmitCommand CommandText="INSERT INTO Articles(Title, UserID) VALUES(@Title, @UserID)" />
  <Label>Title</Label>
  <TextBox Id="Title" DataField="Title" />
</AddForm>

Done.

Simple huh?  We've been able to remove the SelectCommand AND the extra TextBox. The <Variable> tag replaces that. It takes its Value and assigns it to a field called UserID (in this case). The bonus is that the UserID isn't stored "in-the-clear" as with the first method.  In most cases that's a good thing unless you have some Javascript that needs access to it. In those cases you're better off with the Hidden Field approach.

A Parting Gift

We've spent this article talking about saving user data, but you can use these methods to save other data too... Environmental data like the portal's ID ([[Portal:ID]]), the user's IP address ([[Request:HostAddress]]) and other tokens as well as hard-coded values ("blue","bob","253"). It's not just for user data.

Happy XModding!