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>
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.
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>
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
dh2oingAuthor Commented:
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
Squarespace’s all-in-one platform gives you everything you need to express yourself creatively online, whether it is with a domain, website, or online store. Get started with your free trial today, and when ready, take 10% off your first purchase with offer code 'EXPERTS'.
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?
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...
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
Question has a verified solution.
Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.
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...