Skip to main content
March 24, 2011
Question

Passing a url value to a form

  • March 24, 2011
  • 3 replies
  • 1515 views

I am trying to pass a value in a url on to a form.  The url value is called username  (www.whatever.com?username=joe).  I can pass the value on username on to the form with no problem unless username is blank.  Then when the form loads i get the error username is undefined.  How do i get around this?

<cfinput type="text" name="username" message="fill in username " validateat="onSubmit" required="yes" id=username" value="#url.username#" tabindex="3" />

I have tried <cfif isdefined("url.username")> for the value and cant seem to get it right.  I know im on the right track but i am somewhat dead ended.

Thanks in advance.

    This topic has been closed for replies.

    3 replies

    Sreeindia
    Participating Frequently
    March 24, 2011

    Hi

    I hope you are submitting the same form. And when the form loads you dont have any url parameter passed rt?. So you can have the code like following

    <form id="newform" action="www.whatever.com<cfif isdefined("url.username")>?username=joe</cfif>">

    </form>

    Hope this solves your issue

    Inspiring
    March 24, 2011

    The guy's using a CFFORM (note in his example, it's CFINPUT), so one cannot do that with CFFORM.  However one could set the action attribute value to a variable before the CFFORM tag, and use that as the attribute value.

    --

    Adam

    Inspiring
    March 24, 2011
    I have tried <cfif isdefined("url.username")> for the value and cant seem to get it right.  I know im on the right track but i am somewhat dead ended.

    If you are having problems getting code to work... always post the code.  We can't guess what it is you're doing wrong.

    Also be less vague about what the problem with the code is: "can't seem to get it right" isn't really a useful description of the problem ;-)

    What do yuo want to pass if the URL variable has no value?  Do you want to pass the form field with a default value?  An empty string?  Do you perhaps not want to pass it at all?  The answer differs depending on the requirement.

    --

    Adam

    Inspiring
    March 24, 2011

    Initially when your form loads you will not have any URL Parameters right?

    When you submit your form , your are placing the form variable username into the URL.; so it wil be defined at that time.

    March 24, 2011

    No, let me try to explain.

    There are two ways to get to the form reg.cfm. 

    One would be a link that takes you directly to the form reg.cfm with no additional info in the url ie: www.whateversite.com/reg.cfm  In this case, the url.un is undefined.

    The second would be if you were to submit another form where you enter a value and click submit.  That value would then be passed to reg.cfm in which case url.un is defined ie: www.whateversite.com/reg.cfm?un=(formvalue)

    Im am thinking something like:

    <cfinput type="text" name="username" message="fill in username " validateat="onSubmit" required="yes" id="username" <cfif isdifined("url.un")> value="#url.un#" </cfif>  tabindex="3" />

    Inspiring
    March 24, 2011

    You can't nest tags like that.  You have to do your conditional logic beforehand and use the resulting variable in the cfinput tag.  Also, StructKeyExists is a better function than isDefined().

    Alternatively, you can use cfparam so that your url variable always exists.