?
Solved

Setting initial form values w/ CF and JavaScript

Posted on 2004-09-26
8
Medium Priority
?
244 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
6 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 500 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
Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

 

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 500 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What You Need to Know when Searching for a Webhost Provider
When it comes to security, close monitoring is a must. According to WhiteHat Security annual report, a substantial number of all web applications are vulnerable always. Monitis offers a new product - fully-featured Website security monitoring and pr…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses
Course of the Month14 days, 23 hours left to enroll

839 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