#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2012
    Posts
    2
    Rep Power
    0

    Question about code logic and radio buttons in cfform


    hi, I'm relatively new to Coldfusion and I have a problem I can't quite get my head around although this may be quite simple for those more experienced...

    I have a form - two of the fields I collect are the date and time (entered by user) and this gets entered into DateOf and TimeOf fields in my Access database.

    I also want the user to have the option of NOT entering the date and time manually in the form text boxes but also have the form submit hidden date and time as (Now()), IF manually not entered in form...

    This is an either/or situation - the user can entered past dates/times in the text boxes OR the can leave boxes blank and the hidden fields will pass date/time as (Now()).

    My question is this - since both options get entered into same Database fields - DateOf and TimeOf, how best do I have user select either option? I was thinking a radio button selection on the form where option 1 - hidden date/time is passed as Now() to Db or option 2 - manually entered text boxes are passed to Db.

    I need a situation where both are not passed, only one or the other but I can't figure out the correct logic to do this with radio buttons. Any help or suggestions of how to best accomplish this would be greatly appreciated.
    cheers, rob
  2. #2
  3. No Profile Picture
    Moderator

    Join Date
    Jun 2002
    Location
    Raleigh, NC
    Posts
    5,281
    Rep Power
    968
    Why use hidden form fields? I would just create the date and time on the server if the values passed in the form are empty, and use those in the database insert.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2012
    Posts
    2
    Rep Power
    0
    Thanks Kiteless, you rock! - I knew there had to be a simpler way - just couldn't see it! This worked like a charm on the form action page -

    <cfif EditMode>
    <cfparam name="FORM.ID" type="integer">
    <cfupdate (details deleted etc) ...>

    <!--- ADD MODE, if NO ID is passed in the form - assume this is a new addition --->
    <cfelse>
    <!--- now we check to see if date/time fields in form are empty or not --->
    <cfif Form.DateOf IS '' OR Form.Timestamped IS ''>
    <cfset DateNow=#createODBCDate(Now())#>
    <cfset TimestampedNow=#createODBCDateTime(Now())#>
    <!--- dates and time need to be inserted with a query else time doesn't work in DB --->
    <cfquery name="qAddInfo" datasource="#DataSource#">
    INSERT INTO MainTable
    (DateOf,Timestamped)
    VALUES
    (#DateNow#,#TimestampedNow#)
    </cfquery>
    <!--- ... if the date/time fields in the Form are NOT empty, we use those ... --->
    <cfelse>
    <cfquery name="qAddInfo" datasource="#DataSource#">
    INSERT INTO MainTable
    (DateOf,Timestamped)
    VALUES
    (#Form.DateOf#,#Form.Timestamped#)
    </cfquery>
    </cfif>
  6. #4
  7. No Profile Picture
    Moderator

    Join Date
    Jun 2002
    Location
    Raleigh, NC
    Posts
    5,281
    Rep Power
    968
    Hmm, well first, I would never use cfupdate, but that's just me.

    Second, you don't need to do this as separate queries:

    Code:
    <cfif not Len( Trim( form.DateOf )>
    	<cfset form.DateOf = CreateODBCDate( Now() ) />
    </cfif>
    <cfif not Len( Trim( form.Timestamped )>
    	<cfset form.Timestamped = CreateODBCDateTime( Now() ) />
    </cfif>
    
    <cfquery name="qAddInfo" datasource="#DataSource#">
    INSERT INTO MainTable
    (DateOf,Timestamped)
    VALUES
    (#Form.DateOf#,#Form.Timestamped#)
    </cfquery>
    Third, you REALLY should be using cfqueryparam for ALL user-supplied values, to prevent SQL injection exploits.

IMN logo majestic logo threadwatch logo seochat tools logo