• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Convert Form result to a number

Contributor ,
Mar 11, 2019 Mar 11, 2019

Copy link to clipboard

Copied

Hello,

I need to take the value submitted in a form and add 99 to it. I first tried:

<cfset channel.type = 'Form[P#c#Channel]' + 99>

That failed since the form value is a string not a number.

Then I tried:

<cfset channel.type = LSParseNumber('Form[P#c#Channel]') + 99> and got

Form[P1Channel] must be interpretable as a valid number in the current locale.

How can I change the submitted value to a number that I can perform addition on?

Thanks!

Gary

Views

775

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Mar 11, 2019 Mar 11, 2019

Wait a minnit.. are you using 'channel' as a struct or array???  That might be the culprit.  It makes no sense for CF to think '5' is a scalar variable, but it could think 'channel' is a string, not a struct or array.

V/r,

^ _ ^

Votes

Translate

Translate
LEGEND ,
Mar 11, 2019 Mar 11, 2019

Copy link to clipboard

Copied

<cfset channel.type = INT(form['P#c#Channel']) + 99 />

Part of the issue is that you are placing form within the quote, which is what not to do.    Form needs to be outside the string delimiter so that CF knows you're trying to access a value in the form scope.

HTH,

^ _ ^

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Mar 11, 2019 Mar 11, 2019

Copy link to clipboard

Copied

Thank you!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Mar 11, 2019 Mar 11, 2019

Copy link to clipboard

Copied

I probably should have tested it first...(before marking as correct)

<cfset channel.type = INT(form['P#c#Channel']) + 99 />

That returns:

You have attempted to dereference a scalar variable of type class java.lang.String as a structure with members.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Mar 11, 2019 Mar 11, 2019

Copy link to clipboard

Copied

Ignore my last post.  I hate the Jive platform!!!  It's not letting me delete it.  (I had to close the browser and re-logon to finally delete it.)

Try val() instead of int.  Although the scalar error message confuses me.  Are you sure that the form submits a single value?

V/r,

^ _ ^

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Mar 11, 2019 Mar 11, 2019

Copy link to clipboard

Copied

Maybe this will help. I dumped the form values.Screen Shot 2019-03-11 at 4.58.30 PM.png

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Mar 11, 2019 Mar 11, 2019

Copy link to clipboard

Copied

Very odd.  Try LSParseNumber, again, with the quotes in the right place.  If that doesn't work, I don't know.. I've never seen CF declare '5' as a scalar variable.

V/r,

^ _ ^

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Mar 11, 2019 Mar 11, 2019

Copy link to clipboard

Copied

Wait a minnit.. are you using 'channel' as a struct or array???  That might be the culprit.  It makes no sense for CF to think '5' is a scalar variable, but it could think 'channel' is a string, not a struct or array.

V/r,

^ _ ^

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Mar 11, 2019 Mar 11, 2019

Copy link to clipboard

Copied

I changed channel.type to just channel and it works.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Mar 12, 2019 Mar 12, 2019

Copy link to clipboard

Copied

Glad to hear you got it fixed.  And thank you for marking my answer as correct.  I do appreciate it.

V/r,

^ _ ^

UPDATE:  Actually, now that I think of it, if you declare channel a struct, THEN try to save it as "channel.type", that might work.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Mar 12, 2019 Mar 12, 2019

Copy link to clipboard

Copied

LATEST

Try this:

<cfset channel = {} />  <!--- Implicit create --->

or

<cfset channel = StructNew() />  <!--- Expressed create --->

Then start working with your form scope.

<cfset channel.type = lsParseNumber(form['P#c#Channel']) + 99 />

HTH,

^ _ ^

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources
Documentation