ace4u wrote:
> Okay Azadi! You said to shout so....
> Could you please provide me with the most basic syntax
of a form that will populate my db?
sorry for a delay in getting back to you.... been busy with
some stuff
here....
ok... where shall we start?..
first, a form will not populate your db by itself. a form
will only
allow you (user) to enter information you want populated in
your db or
processed in some other way.
a form usually has CONTROLS (i.e. textboxes, checkboxes,
textareas,
select lists, button, etc) that users uses to enter or select
data in
the form. CONTROLS should have NAMES, so data entered into
them can be
accessed on the form's action page (see below). i hope you
are familiar
with basic workings of a form and the <form> and
<cfform> tags - if not,
you better read up on that.
a form SUBMITS when a user clicks on a submit button (or
through another
trigger, i.e. javascript) and sends all the info
entered/selected in it
to the ACTION PAGE, which is specified inside the
<form> tag (if it is
not specified, the form will submit to itself, i.e. to the
same page it
is on). depending on your personal preferences, you can have
a separarte
action page for each form, or have your forms submit to the
same page. i
personally like the latter. in any case, the ACTION PAGE is
where you
validate (using server-side validation; you can also use
client-side
validation, which is performed BEFORE the form is submitted,
on the
actual form's page) and process the submitted information.
a form can submit its data to the action page using either
POST or GET
methods, which are specified in the <form> tag.
default, i believe, is
POST and that is what you should be using. with GET method
all form's
data will be submitted as part of url - not very useful in
most cases...
all the data entered/selected in the form is available on the
action
page in the FORM scope (when POST submission method is used).
to access
individual control's value (data entered into it in the form)
you use
#form.controlname# syntax. some controls have a default value
(even if
nothing has been entered into them - i.e. a textbox always
has a value,
an empty string if nothing has been typed into it in the
form), others
don't, and others may have not a single value, but a
comma-delimited
list of values (i.e. a multi-select list, which is your
regular <select>
tag but with "multiple" attribute in it; or if you have
several controls
in your form with the same name then the form scope will hold
a
comma-delimited list of values of all those controls in one
form.controlname variable). some controls (i.e. checkboxes)
will not be
submitted with the form (and thus will not be in the FORM
scope) if they
were not selected by user.
to populate your db with form's data you use <cfquery>
tags with INSERT
or UPDATE sql statements depending on if you are inserting
new data or
updating existing data. if you are not familiar with sql
INSERT and
UPDATE statements you better read up on those, too. a book by
Ben Forta
called something like "Teach yourself SQL in 10 minutes" has
been
recommended a lot in these forums + there are a ton of
websites with
tutorials and instructions. important thing here is that what
you enter
into your db table and how depends on what datatype your
table column
is... main point: any values in your insert/update queries
that go into
TEXT fields in your db table should be surrounded with '
(single
quotes), while those that go into INTEGER (number) fields
should not:
i.e. use '#form.textvalue#' and #form.integervalue# - see
examples below
for details.
there are a multitude of other things you should know about
forms and
sql statements, but i will leave you to learn them as you go.
so,
without further ado, here is a simple example. i have
purposefully NOT
made it based on your tables, so you can practice and make
the form and
action page for your specific needs yourself :)
===================================================
form page (form.cfm):
<!--- some basic form with a number of different controls
--->
<form action="actionpage.cfm" method="post">
First Name: <input type="text" name="firstname"
size="50"><br>
Last Name: <input type="text" name="lastname"
size="50"><br>
Your Gender: <input type="radio" name="gender" value="M"
checked>Male
<input type="radio" name="gender"
value="F">Female<br>
Your Age: <input type="text" name="age"
size="2"><br>
Your Interests:
<select name="interests" size="5" multiple>
<option value="movies">Movies</option>
<option value="theatre">Theatre</option>
<option value="ballet">Ballet</option>
<option value="opera">Opera</option>
<option value="coldfusion">CF
programming</option>
<br>
<input type="submit" name="submit" value="save">
</form>
====================================================
action page (actionpage.cfm):
<!---
here is where we process submitted form data.
this example is for INSERTING new data into db, not updating
data.
this example inserts data into 2 tables: basic data (name,
gender, age)
is inserted into one table, while selected interests are
inserted into
second table which is linked to the first table through an
FK.
tables structure:
tblUserData
(UserID [PK, autonumber], Firstname [text], Lastname [text],
Gender
[text], Age [number])
tblUserInterests
(userinterestID [PK, autonumber], userID [FK to
tblUserData.userID,
number], interestName [text])
--->
<cfif isdefined("form.save")><!--- check that a form
has been submitted
and we have FORM scope available to us --->
<!--- insert basic data into tblUserData --->
<cfquery name="insertData" datasource="yourdsn">
INSERT INTO tblUserData (Firstname, Lastname, Gender, Age)
VALUES ('#form.firstname#', '#form.lastname#',
'#form.gender#',
#form.age#)
</cfquery>
<!--- check if any interests have been selected in the
form
and if they have, enter them into tblUserInterests --->
<cfif isdefined("form.interests") AND
len(trim(form.interests)) gt 0>
<!--- get inserted user's userID --->
<cfquery name="getuserid" datasource="yourdsn">
SELECT MAX(userID) as userid FROM tblUserData
</cfquery>
<!--- loop through submitted interests and insert them
--->
<cfloop list="#form.interests#" index="interestName">
<cfquery name="insertInterest" datasource="yourdsn">
INSERT INTO tblUserInterests (userID, interestName)
VALUES (#getuserid.userid#, '#interestName#')
</cfquery>
</cfloop>
</cfif>
<cflocation
url="whereveryouwanttogoafterinsertingdataintoyourdb"
addtoken="no">
</cfif>
==============================================================
the above example is far from perfect, but it should set you
on your
way. as always, the forums are here if you hit a roadblock...
hope this helps...
--
Azadi Saryev
Sabai-dee.com
http://www.sabai-dee.com