Solved

Setting initial form values w/ CF and JavaScript

Posted on 2004-09-26
8
241 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

 

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

Create the perfect environment for any meeting

You might have a modern environment with all sorts of high-tech equipment, but what makes it worthwhile is how you seamlessly bring together the presentation with audio, video and lighting. The ATEN Control System provides integrated control and system automation.

Question has a verified solution.

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

If you don't have the right permissions set for your WordPress location in IIS, you won't be able to perform automatic updates. Here's how to fix the problem.
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…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

726 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