Sharpharp1
Apprentice

Get IP address of user in Smartform?

Hi,

 

Is there any way of getting the users IP address when they open a smartform?

 

I don't want to install any third party k2 add-ons, is there a way to get it using Javascript/Jquery used in a data label on a View/form?

 

Any help appreciated.

 

Thanks

0 Kudos
Reply
5 Replies
Mivaro1
Apprentice

Re: Get IP address of user in Smartform?

Hello  @Sharpharp1 


 


Basically this script retrieves back the client IP and sets it into a data label with name in yellow. 


Note that NAT still causes the IP to show the external network interfaces IP. Not the computers IP. 


 


Method 


Add the following in a data label with the literal property selected 


 


<script type="text/javascript">$.getJSON("http://jsonip.com?callback=?", function (data) {SourceCode.Forms.Controls.Web.Label._getInstance($("span[name='Data Label']").attr("id")).SFCLabel('option', "text", data.ip);});</script> 


 

0 Kudos
Reply
Sharpharp1
Apprentice

Re: Get IP address of user in Smartform?

Hi Mivaro

 

Thanks for the script. The scrip displays the IP address, but then how do you save it?

 

If i try and do a create method and include the data label, it tries to save the script, rather than the actual IP address.

 

If IP is a Text field in the smo, you get error: "The property has a long value for type Text. the maximum length allowed is 100 characters"

 

Any ideas?

0 Kudos
Reply
Mivaro1
Apprentice

Re: Get IP address of user in Smartform?

I just added a transfer data rule from the datalabel to a textbox and was able to save the text value only. Perhaps try that?


 

0 Kudos
Reply
Sharpharp1
Apprentice

Re: Get IP address of user in Smartform?

Hi,

 

Still not having it with transferring datalable to textbox

 

My setup is as follows:

 

1) On Form canvas i have an empty text box and a datalable with the expression containing your script... This displays the IP fine

 

2) Then i have a rule When the form is initialized

Transfer data - transfer the data label to the text box

 

This does not work - it transfers the script to the textbox

 

EDIT: Did a few tests, if i add a button and on the form and use that to do the transfer it works, so it must be LAG when your script is running. Using the initialized rule to do the transfer, i can visibily see the transfer taking place before the IP address appears in the data label... would this cause the script to be copied over rather than the value?

 

Also tried using teh Initalized rule to do the transfer, that doesn't work either.

0 Kudos
Reply
NigelStratton
Apprentice

Re: Get IP address of user in Smartform?

The reason you got the script, rather than the IP, is the script gets dropped into the data label and executed. While the script is running, you transfer its value to the text box. The script finishes and updates the value of the data label, but too late as your speedy form has already transferred the previous value of the data label.

 

If you want this to work, add a When Data Label is changed rule to transfer the IP value, but this script directly targets a Text Box called "Text Box". You can skip the "SourceCode.Forms.Controls.Web.Label._getInstance ...attr('id')", this is the same.

 

<script type="text/javascript">

    $.getJSON("https://jsonip.com?callback=?",

        function (data)

        {$("[name='Text Box']").SFCTextBox('option', "text", data.ip);});

</script>

 

A final tip, if you want to run a script more than once, you need two data labels, one with the script and a blank destination. Each time you blank the destination and transfer the script, it will run the script again.

0 Kudos
Reply