Nate
Nate

Reputation: 23

ColdFusion Forms

I'm trying to make a form that accesses a MySQL database. But all of this is new to me. I think I might be trying to reach too far beyond my basic understanding of this. Here is my question.

I have a form. In this form I have the connection made to the database and am able to post to the database. What I'm trying to do is have an option field with "tile 1", "tile 2", "tile 3". For each "tile" I'd like to have the rest of the fields saved under that tile ID. I'd also like to be able to pull that tiles information when selecting the tile from the option field.

Here is my current code. Please keep in mind I'm new to all of this. Thanks.

Form:

<cfform method="post" name="TileAdAdmin" action="index.cfm">
          <cfoutput>
              <table width="500px">
              <tr>
                <td height="159"><label class="labelStyle">Tile Ads</label></td>
                <td>
                  Tile Number:<select name="TAID" id="TAID">
                    <option value="0">None</option>
                    <option value="1">Tile 1</option>
                    <option value="2">Tile 2</option>
                    <option value="3">Tile 3</option>
                  </select><br /><br />
                  Headline:
                   <input name="TAHL" type="text" id="TAHL" title="TAHL" value="#variables.ta.TAHL#" maxlength="30" />
                   <br />
                   Image Name: 
                   <input name="TAImage" type="text" id="TAImage" title="TAImage" value="#variables.ta.TAImage#" maxlength="30" />
                   <br />
                   Discription:                   
                   <textarea name="TADiscription" cols="30" rows="6" id="TADiscription" title="VimeoID">#variables.ta.TADiscription#</textarea>                   <br />
                  </td>
              </tr>
            </table><br />

            <input type="hidden" value="#variables.controller#" name="controller">
            <input type="hidden" value="#variables.action#" name="action">
            <input type="Submit" value="Submit" name="Submit" class="formInput">
          </cfoutput>
        </cfform>

DAO Code:

    <cfcomponent name="TADAO" displayname="" hint="" output="false">

  <cffunction name="read" access="public" returntype="Void" output="false" hint="CRUD method">
    <cfargument name="ta" type="ta" required="yes" />

    <cfset var qRead = 0 />
    <cfquery name="qRead" datasource="#Application.dbsource#" >
      SELECT
        TAID,
        TAHL,
        TAIMAGE,
        TADISCRIPTION
      FROM
        T026_TILE_ADS
    </cfquery>

    <cfif qRead.RecordCount>
      <cfset arguments.ta.setTAID(qread.TAID) />
      <cfset arguments.ta.setTAHL(qread.TAHL) />
      <cfset arguments.ta.setTAImage(qread.TAIMAGE) />
      <cfset arguments.ta.setTADiscription(qread.TADISCRIPTION) />
      <cfelse>
      <cfthrow type="emptyRecordset" errorcode="TADAO.read.emptyRecordset" message="Unable to locate the TILE_AD data record." />
    </cfif>
  </cffunction>

  <cffunction name="update" access="public" returntype="Void" output="false" hint="CRUD method">
    <cfargument name="TA" type="TA" required="yes" />

    <cfquery name="qUpdate" datasource="#Application.dbsource#">
      UPDATE
        T026_TILE_ADS
      SET
        TAID = <cfqueryparam cfsqltype="cf_sql_numeric" value="#arguments.ta.getTAID()#" />,
        TAHL = <cfqueryparam cfsqltype="cf_sql_varchar" value="#arguments.ta.getTAHL()#" />,
        TAIMAGE = <cfqueryparam cfsqltype="cf_sql_varchar" value="#arguments.ta.getTAImage()#" />,
        TADISCRIPTION = <cfqueryparam cfsqltype="cf_sql_text" value="#arguments.ta.getTADiscription()#" />
     </cfquery>

  </cffunction>

</cfcomponent>

Component code:

<cfcomponent displayname="ta" hint="" output="false">

<cfproperty name="TAID" displayname="" type="numeric" />
<cfproperty name="TAHL" displayname="" type="string" />
<cfproperty name="TAImage" displayname="" type="string" />
<cfproperty name="TADiscription" displayname="" type="string" />


<cffunction name="init" access="public" returnType="TA" output="false" hint="">
  <cfset this.TAID = 0 />
  <cfset this.TAHL = "headline" />
  <cfset this.TAImage = "IMAGE" />
  <cfset this.TADiscription = "Discription" />

  <cfreturn this />
</cffunction>

<cffunction name="getTAID" access="public" output="false" returntype="numeric">
    <cfreturn this.TAID />
</cffunction>

<cffunction name="setTAID" access="public" output="false" returntype="void">
    <cfargument name="TAID" type="numeric" required="true" />
    <cfset this.TAID = arguments.TAID />
    <cfreturn />
</cffunction>

<cffunction name="getTAHL" access="public" output="false" returntype="string">
    <cfreturn this.TAHL />
</cffunction>

<cffunction name="setTAHL" access="public" output="false" returntype="void">
    <cfargument name="TAHL" type="string" required="true" />
    <cfset this.TAHL = arguments.TAHL />
    <cfreturn />
</cffunction>

<cffunction name="getTAImage" access="public" output="false" returntype="string">
    <cfreturn this.TAImage />
</cffunction>

<cffunction name="setTAImage" access="public" output="false" returntype="void">
    <cfargument name="TAImage" type="string" required="true" />
    <cfset this.TAImage = arguments.TAImage />
    <cfreturn />
</cffunction>

<cffunction name="getTADiscription" access="public" output="false" returntype="string">
    <cfreturn this.TADiscription />
</cffunction>

<cffunction name="setTADiscription" access="public" output="false" returntype="void">
    <cfargument name="TADiscription" type="string" required="true" />
    <cfset this.TADiscription = arguments.TADiscription />
    <cfreturn />
</cffunction>

 <cffunction name="dump" access="public" output="true" returntype="void">
  <cfoutput>
    Tile:         #this.TAID# <br />
    Headline:         #this.TAHL# <br />
    Image:         #this.TAImage# <br />
    Description:         #this.TADiscription# <br />
    </cfoutput>
</cffunction>

Upvotes: 1

Views: 312

Answers (1)

Niloct
Niloct

Reputation: 10015

TAID as a primary key

TAID is the identifier of each of your tiles, so first of all you need to add it to a WHERE clause in the SQL statements.


  UPDATE
    T026_TILE_ADS
  SET
    TAHL = <cfqueryparam cfsqltype="cf_sql_varchar" value="#arguments.ta.getTAHL()#" />,
    TAIMAGE = <cfqueryparam cfsqltype="cf_sql_varchar" value="#arguments.ta.getTAImage()#" />,
    TADISCRIPTION = <cfqueryparam cfsqltype="cf_sql_text" value="#arguments.ta.getTADiscription()#" />
  WHERE
    TAID = <cfqueryparam cfsqltype="cf_sql_numeric" value="#arguments.ta.getTAID()#" />

  SELECT
    TAID,
    TAHL,
    TAIMAGE,
    TADISCRIPTION
  FROM
    T026_TILE_ADS
  WHERE
    TAID = <cfqueryparam cfsqltype="cf_sql_numeric" value="#arguments.ta.getTAID()#" />

Calling the update query before the select query

Update: after analysing your database code, the order of definition of cfquerys is not important since they are inside CFFUNCTIONS of the CFCOMPONENT.


Selecting OPTION with Jquery

Finally you will need to select the tile from the submitted form, you can spice your code with Jquery (add inside, at the end of the CFOUTPUT)


<!--- (beginning of CFOUTPUT ...) --->
<input type="Submit" value="Submit" name="Submit" class="formInput">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
<script>
$(document).ready(function(){
   $('SELECT##TAID').val(#variables.ta.TAID#);
});
</script>
</cfoutput>

Edit 26/07

Using <CFSELECT> instead

In the CFOUTPUT the TAID field is a simple HTML SELECT. Since we want to pull records from the DB, try this:

Put before the CFOUTPUT tag:

<cfquery name="qryTiles" datasource="#Application.dbsource#">
  SELECT
    TAID,
    TAHL,
    TAIMAGE,
    TADISCRIPTION
  FROM
    T026_TILE_ADS
</cfquery>

And replace:

<select name="TAID" id="TAID">
<option value="0">None</option>
<option value="1">Tile 1</option>
<option value="2">Tile 2</option>
<option value="3">Tile 3</option>
</select>

with:

<cfselect name="TAID"
 size=1
 multiple="no"
 query="qryTiles"
 value="TAID"
 display="TAID"
 queryPosition="below">
  <option value="0">None</option>
</cfselect>

Upvotes: 1

Related Questions