On Click Counter

  • 0
  • 1
  • Question
  • Updated 4 weeks ago
  • Answered
Two years ago, @lin posted requesting help creating an on click counter. Mark and Dan shared solutions and the post has been closed to commenting so I am posting a new post, post post. When I use the solution Mark provided and am in edit mode on a form, the on click button only increments by one no matter how many times it is clicked and only after the page is refreshed. When I use Dan's solution, I can't get passed syntax errors. I would appreciate any help and/or suggestions. 

Mark provided this solution:

var number NewValue = [My counter field] +1;

var text URL = urlroot() & "db/" & dbid() & "?act=API_EditRecord&rid=" & [Record ID#]
& "&_fid_99=" & ToText($NewValue);

"javascript:" &
"$.get('" & 
$url & 
"',function(){" &
"$.jGrowl('Children have been added', {life: 5000, theme: 'jGrowl-green'});" &
"});" &
"void(0);"

// replace the 99 with the field ID of the counter field.

Dan provided this solution:

var dbidTable = "YOUR DBID";
var apptoken = "YORU APPTOKEN";
$.ajaxSetup({data: {apptoken: apptoken}});

var fid = 6;  // [counter] field

$.get(dbidTable, {
  act: "API_DoQuery",
  query: "{3.EX." + kRid + "}",
  clist: fid
}).then(function(xml) {
  console.dirxml(xml);
  
  var oldValue = parseInt($("counter", xml).text());
  var newValue = oldValue + 1;
  console.log(oldValue, newValue);
  
  $.post(dbidTable, {
act: "API_EditRecord",
rid: kRid,
_fid_6: newValue
  }).then(function(xml) {
// done
  })
});
Photo of Adam Keever

Adam Keever

  • 1,904 Points 1k badge 2x thumb
  • cautiously optimistic

Posted 4 weeks ago

  • 0
  • 1
If you want the page to refresh after each click to see the counter increase, then you would use this

var number NewValue = [My counter field] +1;

var text URL = urlroot() & "db/" & dbid() & "?act=API_EditRecord&rid=" & [Record ID#]
& "&_fid_99=" & ToText($NewValue);




"javascript:" &
"$.get('" & 
$URL & 
"',function(){" &
"location.reload(true);" &
"});" 
& "void(0);"


Photo of Adam Keever

Adam Keever

  • 1,904 Points 1k badge 2x thumb
Thanks Mark. I tried that and it does not increment for me, but does refresh. So I added the apptoken and it continued to not increment ,but then also stopped refreshing.

Is there a syntax guide for formula URL and formula rich text somewhere?

I added the apptoken like this:

var text URL = URLRoot() & "db/" & Dbid() & "?act=API_EditRecord &apptoken=xxxxxxxxxxxxxxxxxxxxxxx &rid=" & [Record ID#]
& "&_fid_99=" & ToText($NewValue);

I also noticed that my counter field is numeric and there is a function to turn NewValue ToText. Should I be using a text field; I thought the mathematical addition operator would not funtion with text.
(Edited)
I find it hard to believe that it does not increment. Are you updating the correct field in your app. Can you post your actual code. For example it’s highly unlikely that the field do you want to update his field ID 99
Also, when you’re trying to debug these what you do is you just run the pure URL without the JavaScript refresh and then it will reveal any error messages.
Photo of Adam Keever

Adam Keever

  • 1,904 Points 1k badge 2x thumb
My increment field is fid 686. I did change it and it does not function.

Here is the code revised for my fields:

var number NewValue = [test counter field] +1;

var text URL = URLRoot() & "db/" & Dbid() & "?act=API_EditRecord&apptoken=xxxxxxxxxxxxxxxxxxxxxxx&rid=" & [Record ID#]
& "&_fid_686=" & ToText($NewValue);

When I delete the JavaScript, there is an error stating: column: 0 > Expecting text

(Edited)
Add

$URL

As the last line so it runs the URL

When our run it,, it will either come back with a success or error message.
Photo of Adam Keever

Adam Keever

  • 1,904 Points 1k badge 2x thumb
Did that and got an error about the app token so I turned off require apptoken and got this message:



When I went back to the record it did increment.

Looks like it was an issue with the apptoken. I was trying to use one that was created as not ok to copy. Added a new token and all is good with the world.

Thanks for the feedback and direction Mark!
(Edited)
OK, so you are all good now.  You see that by running the URL raw you get to see the successor error message. 

When you have that javascript refresh code enabled, your button could have started WWIII with nuclear war and the screen would just quietly refresh with no indication of a problem.  So you run the raw button until it works and then enable the refresh.