Solved

Setting initial form values w/ CF and JavaScript

Posted on 2004-09-26
8
233 Views
Last Modified: 2013-12-24
Ok, this should be easy, but I have a way of making the easiest things the hardest. Now to the situation at hand. I am creating a coldfusion site to sell a product. For each product, the site admin needs to be able to edit item info. Right now I have the site set up to use the same page to both add and edit items. The form shows blank to add an item (this obviously works correctly), but should contain existing data for an item to be edited. This is where I am having problems. I am trying to set the value element of the input field on the forms. Here is my code;

<cfif #url.a# eq "e">
      <cfquery name="getItemInfo" datasource="ninaDB">
      Select *
      From table
      Where item_id = <cfqueryparam cfsqltype="cf_sql_integer" maxlength="2" value="#url.i#">
      </cfquery>
      
      <script language="JavaScript" type="text/JavaScript">
            document.theForm.item_id.Value = <cfoutput>#getItemInfo.item_id#</cfoutput>;
            document.theForm.Rname.Value = "<cfoutput>#getItemInfo.item_name#</cfoutput>";
            document.theForm.Rcat_id.value = "<cfoutput>#getItemInfo.cat_id#</cfoutput>";
            document.theForm.Rprice.value = "<cfoutput>#getItemInfo.price#</cfoutput>";
            document.theForm.shipping.value = <cfoutput>#getItemInfo.shipping#</cfoutput>;
            document.theForm.Rdesc_shrt.value = <cfoutput>#getItemInfo.desc_shrt#</cfoutput>;
            document.theForm.Rdesc_long.value = <cfoutput>#getItemInfo.desc_long#</cfoutput>;
            document.theForm.Rpicture_sml.value = <cfoutput>#getItemInfo.picture_sml#</cfoutput>;
            document.theForm.Rpicture_lrg.value = <cfoutput>#getItemInfo.picture_lrg#</cfoutput>;
            document.theForm.isSold.value = <cfoutput>#getItemInfo.isSold#</cfoutput>;
            document.theForm.isListed.value = <cfoutput>#getItemInfo.isListed#</cfoutput>;
            document.theForm.isFeatured.value = <cfoutput>#getItemInfo.isFeatured#</cfoutput>;
</script>
</cfif>

url.a is the action variable that determines if the page is for adding or editing an item. In the source code for the page, the cfoutputs work correcting (displaying the contents of the variable), but I keep getting an error about a missing semi-colon, which I don't understand.

I know this is an idiots question,

BUT PLEASE HELP!!!

Thanks,
Josh
0
Comment
Question by:dh2oing
  • 3
  • 2
8 Comments
 
LVL 17

Expert Comment

by:Tacobell777
ID: 12156560
wow wow, you are mixing JS with CF, not a good idea in this case.

Try

<cfparam name="form.firstName" default="" type="string" />

<cfoutput>
<form method="post" action="">
<input type="text" name="firstName" value="#form.firstName#" />
</form>
</cfoutput>

enter some data
now submit the form
you'll notice that the form is populated with the entry you put in there...
this is good if there was an error and you wanted to display the data to the user again so he/she can correct it.

Now for the edit
same page

<cfquery name="qData" datasource="yourdatasource">
SELECT firstname
FROM yourtable
WHERE (x = y)
</cfquery>

<cfset form.firstname = qData.firstName />

<cfparam name="form.firstName" default="" type="string" />

<cfoutput>
<form method="post" action="">
<input type="text" name="firstName" value="#form.firstName#" />
</form>
</cfoutput>

There is an easier way to populate the form scope again, but we'll get to that when you understand the above concept...
0
 
LVL 17

Accepted Solution

by:
Tacobell777 earned 125 total points
ID: 12156571
just incase you do not care about the above solution:

<cfoutput>
 <script language="JavaScript" type="text/JavaScript">
          document.theForm.item_id.Value = #getItemInfo.item_id#;
          document.theForm.Rname.Value = "#getItemInfo.item_name#";
          document.theForm.Rcat_id.value = "#getItemInfo.cat_id#";
          document.theForm.Rprice.value = "#getItemInfo.price#";
          document.theForm.shipping.value = #getItemInfo.shipping#;
          document.theForm.Rdesc_shrt.value = #getItemInfo.desc_shrt#;
          document.theForm.Rdesc_long.value = #getItemInfo.desc_long#;
          document.theForm.Rpicture_sml.value = #getItemInfo.picture_sml#;
          document.theForm.Rpicture_lrg.value = #getItemInfo.picture_lrg#;
          document.theForm.isSold.value = #getItemInfo.isSold#;
          document.theForm.isListed.value = #getItemInfo.isListed#;
          document.theForm.isFeatured.value = #getItemInfo.isFeatured#;
</script>
</cfoutput>
make sure you have a ; at the end of each statement, and use ' around string values
0
 

Author Comment

by:dh2oing
ID: 12156624
I tried the second solution, but I still get an error: Expected: ';' Line: 22 Column: 45. (that is right after the '=' on the first document.form...
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 

Author Comment

by:dh2oing
ID: 12156634
I am still a little confused on solution one offered above, what is the concept? I understand using the cf variable and bypassing the js. Should I just use a cfswitch statement to set the cf variables to "" for a new item or the existing data for editings?
0
 
LVL 17

Expert Comment

by:Tacobell777
ID: 12156677
Value probably needs to be lowercase

the concept of the solution is, that you post a form to itself, if there are any errors the form is populated again and the user can correct, the same form can also be reused for editing records...
0
 
LVL 9

Assisted Solution

by:digicidal
digicidal earned 125 total points
ID: 12186416
Why not just put the logic in your form fields value attribute?  Ex.

<cfif #url.a# eq "e">
     <cfquery name="getItemInfo" datasource="ninaDB">
     Select *
     From table
     Where item_id = <cfqueryparam cfsqltype="cf_sql_integer" maxlength="2" value="#url.i#">
     </cfquery>
</cfif>
<cfoutput>
<input type="text" size="20" name="item_id" value="<cfif IsDefined('getItemInfo.Item_id')>#getItemInfo.Item_id#</cfif>">
...
</cfoutput>

That way they are blanks if not defined, and filled if they are... if you want to provide default values just make the <cfif></cfif> a <cfif><cfelse></cfif> and put the default value in the second slot.

Not sure this is the best solution, but it's definitely an easy one and will eliminate the Javascript entirely.  This will slightly slow the server compile of the page (but caching will solve that on 2nd load), but it will actually make it slightly faster on the client side since the javascript doesn't have to fire... also this will work on browsers that have javascript turned off as well.

Just a thought.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This is a guide to setting up a new WHM/cPanel Server to be used for web hosting accounts. It is intended for web hosting company administrators and dedicated server owners. For under $99 per month (considering normal rate of Big Data Cetnters like …
In our day to day coding, how many times have we come across a necessity to check whether a URL is a broken link or not? For those of you that answered countless and are using ColdFusion like myself, then this article is for you.  It will show yo…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now