• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 328
  • Last Modified:

Multiple list of form itsm doing entering into database

Hi, I have the form elements which are like this:

ping_0 sfgsdfsdf  
ping_1 [empty string]  
ping_10 [empty string]  
ping_11 [empty string]  
ping_12 [empty string]  
ping_13 [empty string]  
ping_14 [empty_string]

Open in new window


THE database is ORACLE/Coldfusion 8
the list can go n level long.

How i am trying is for a specific checkbox which will have the ping_12 as listed against it and ping_12 also listed against it and so as values_12 ..

So suppose i checked 10 checboxes and the i will pass all the value of from ping_12 to ping_24..

Then how i handle this through structs to enter the value in the Database!

For each checkbox there is one row

Please guide  
0
Gurpreet Singh Randhawa
Asked:
Gurpreet Singh Randhawa
  • 13
  • 12
1 Solution
 
gdemariaCommented:
Here is your action statement loop.   Do you know how many records there will be?   You should pass that over if you don't have it already.



<cfloop index="ctr" from="1" to="#numberRecords#">
   <cfif structKeyExists(Form,"ping_#ctr#")>
       <cfset theValue = form["ping_#ctr#"]>
       <cfif len(trim(theValue))>
             .... do insert here using #theValue# .....
        </cfif>
   </cfif>
</cfloop>


0
 
Gurpreet Singh RandhawaWeb DeveloperAuthor Commented:
Actually the structure is quite complex as i am examining more, This kinda stuff i am doing very first time!

I have a checbox and i select its value, the remaining fields become enabled, otherwise they stay disabled!

1. disabled values do not go to server side!
So at least 1 value has to be checked and its dependent values will go to the server side page!

3. The fields which are going to next have four fields and they have dynamic values attached to them, if its radio button, yes/no, the name will be same for them.

4. how i can capture them in front because they will passed as multiple if two checkboxes are selected!, checkbox name is static and value if selected goes as comma separated, other values goes as by their dynamic names and values.

so basically

textfield, radio and textarea fields are there which will be going to server side processing!

Can u guide me in good way
0
 
Gurpreet Singh RandhawaWeb DeveloperAuthor Commented:
It is basically like this!

[]Checkbox      textfield        radiobutton             radiobutton              textarea

now i check the checkbox and the values against it gtets enabled, all of them

Now i pass them to the action action!

[] - Checkbox name is not dynamic, it is named as checkedvalues

all the remaining fields are dynamic as loop is running over them and they are named as:

textfield_1_2

so basically this

1_2 is dynamic for all the remaing three, i means all the three fields will bear the 1_2 against them as they are on the same row!

Well to me this i am doing it first time, so i am bit confused how should i approch it!

Please guide
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
gdemariaCommented:


I would name your fields like this...


 <input type="text" name="FirstName1"  ....

 <input type="text" name="LastName1"  ....

 <input type="radio" name="Gender1" value="M" ....
 <input type="radio" name="Gender1" value="F" ....

Every field has a ROW counter after the field name.   But the field name is a good name that means whatever it is... FirstName, Gender, Date, Description, etc...


I don't know why you have _1_2    it seems you only need _1

Make sense?

0
 
Gurpreet Singh RandhawaWeb DeveloperAuthor Commented:
yes i understand they are like this

n1
    its value like textfield, checkbox, others
      a1
        its value like textfield, checkbox, others
          c1
            its value like textfield, checkbox, others


it is all nested within a loop and nested loops! a complex structure!

Concerned only c1 as if its value is checked and corrosponding other like c32,c3 values are checked, then only their value is going to in the DB, so we are safe here

but what is the exact approach i am really confused
0
 
gdemariaCommented:

it sounds like you said something different from me...

Do we agree that your form will look like this...


  A 1           B 1           C 1               D 1              E 1

  A 2           B 2           C 2               D 2              E 2

  A 3           B 3           C 3               D 3              E 3



A, B, C, D and E are the names of the fields.   The fields can be anything text, checkbox, radio, it doesn't matter

1,2,3,... are different ROWS and in the database each number will be a different record

So,  A, B, C, D and E for 1 will update Row 1 of the database,
 A, B, C, D and E for 2 will update Row 2 of the database, etc...

Do you agree?

If yes, I will show you how to process this.    If  not, please tell me how your form is different from this.   Please provide an example that makes sense...



0
 
Gurpreet Singh RandhawaWeb DeveloperAuthor Commented:
yes somewhat similar!

checked value will have the fields enabled and send their value to the table for insertion, if i check another checbox, values of its get enabled and send to the server page

I hope we can come on to this! if we break anywhere i will keep u updated here!

values are dynamically going
0
 
gdemariaCommented:

Ok, there are two seperate parts

1) the checkbox - when you check this box, some javascript will enable/disable the row of fields

2) setting up the rows of fields and updating the database with those values


First the checkbox...

function setRow (id, onOff) {
   
   document.getElementById('firstname_' + id).style.disabled = onOff;
   document.getElementById('lastname_' + id).style.disabled = onOff;
   document.getElementById('gender_' + id).style.disabled = onOff;

}


Then call the function on each checkbox row...
<input type="checkbox" onClick="setRow(#ctr#,this.checked);" > 

0
 
Gurpreet Singh RandhawaWeb DeveloperAuthor Commented:
fields are already being enabled/disabled,

lets concentrate that fields are enabled because at the moment, one checkbox has to be selected and go through it and if it is selected

the values will pass either default as no, and null to textarea, just need server side handling how to do
0
 
gdemariaCommented:
> fields are already being enabled/disabled,

oh, you kept saying it so I thought that is where you needed the help first

> just need server side handling how to do

here is the starting code..

  we can make it smarter to validate and delete as we ll..
<cfloop index="ii" from="1" to="#form.totalRecords#">
  <cfset variables.firstName = form['firstName' & kk]>
  <cfset variables.lastName  = form['lastName' & kk]>
  <cfset variables.username  = form['username' & kk]>
  <cfset variables.userID    = form['userID' & kk]>

  <cfif len(variables.userID)> <!--- update --->
	  <cfquery name="UpdateUser" datasource="DSN">
	    update user
		  set firstName = '#variables.firstName#'
		    , lastName  = '#variables.lastName#'
			, userName  = '#variables.username#'
		 where userID = #val(variables.UserID)#
	  </cfquery>
  <cfelse> <!--- no ID, so insert --->
	  <cfquery name="UpdateUser" datasource="DSN">
	    insert into user (firstname, lastname, username)
		values ('#variables.firstName#','#variables.lastName#','#variables.username#')
	  </cfquery>
  </cfif>  
</cfloop>

Open in new window

0
 
Gurpreet Singh RandhawaWeb DeveloperAuthor Commented:
I think i am unable to show you the exact behavious, i present here visually

<cfloop from="1" to="5" index="o">
	<input type="checkbox" name="chedValue" id="id#o#"><br>
    <input type="text" name="textfield" id="textfield_#o#" />
    	<input type="radio" name="radio1_#o#" value="yes" />Yes
    	<input type="radio" name="radio1_#o#" value="No" />No
    	
        <input type="radio" name="radio2_#o#" value="yes" />Yes
    	<input type="radio" name="radio2_#o#" value="No" />No
    <textarea name="insert_#o#" id="insert_#o#"></textarea>
        
      <cfloop from="1" to="3" index="k">
      	<input type="checkbox" name="chedValue" id="id#o#_#k#"><br>
    	<input type="text" name="textfield" id="textfield_#o#_#k#" />
    	<input type="radio" name="radio1_#o#_#k#" value="yes" />Yes
    	<input type="radio" name="radio1_#o#_#k#" value="No" />No
    	
        <input type="radio" name="radio2_#o#_#k#" value="yes" />Yes
    	<input type="radio" name="radio2_#o#_#k#" value="No" />No
    	<textarea name="insert_#o#_#k#" id="insert_#o#_#k#"></textarea>
      </cfloop>  
</cfloop>

So here above you see its is 2 nested level , in my case it is 3 level deep and the loop in the alst level has checkbox who will be checked at the end. 

1. checked is checked
2. textfieldis empty, all the field which appears in line are disabled yet!
3. enter value in textfield and the remaining fields get enabled for that specific checkboxes

now i want to capture the enabled checkbox and textfield and the radio and textareas and if they appear disabled make their value as default to no for that specific row which is checked to be inserted into DB

Open in new window


If still needs help please tell me i can create an image for this and show you

i am really in ned of help of an Guru in this case because this is something which i had not done and keenly interested in doing such thing
0
 
gdemariaCommented:

We had agreed that your input tag names would be "name_#row#"  

that is not what you are doing, you are doing  "radio1_#o#_#k#"

I don't know why you want two variables in the name, you only need one.    Give the field a good name such as Name, Address, City, State, Product, Quantity, etc...  and then follow it by the row number #row#

0
 
Gurpreet Singh RandhawaWeb DeveloperAuthor Commented:
well let's be simple, we should leave the naming conventions right off the road, I agree on this and sorry for the names being irerelavant, just used itas because nested loop with add an extra step to differ it from the above loop
0
 
gdemariaCommented:

It seems your form is going to populate at least two different tables.  Is that right?

The upper part will populate table 1  (let's call it MAIN)

    <input type="checkbox" name="chedValue" id="id#o#"><br>
    <input type="text" name="textfield" id="textfield_#o#" />
    <input type="radio" name="radio1_#o#" value="yes" />Yes
    <input type="radio" name="radio1_#o#" value="No" />No

And then you have an inner loop which will populate table B  (let's call it CHILD)..

    <cfloop from="1" to="3" index="k">
            <input type="checkbox" name="chedValue" id="id#o#_#k#"><br>
          <input type="text" name="textfield" id="textfield_#o#_#k#" />
          <input type="radio" name="radio1_#o#_#k#" value="yes" />Yes
          <input type="radio" name="radio1_#o#_#k#" value="No" />No


Important Question -

 Will your form be populated with data from the database?  OR is it a blank for that will be used to INSERT only ?

 I need to know if we must populate the form and provide UPDATE as well as inssert..

thanks
0
 
Gurpreet Singh RandhawaWeb DeveloperAuthor Commented:
ok! the meat of the form!

a The data will go in single table only,

1. The four fields which is the textfield, radio,radio and textarea will get entered into the table for each checked checkbox, if the checkbox is checked and the textfield value is null - all remaining three fields are disabled so there default value should go as no, no, ""

2.  The four fields which is the textfield, radio,radio and textarea will get entered into the table for each checked checkbox, if the checkbox is checked and the textfield value is not null - all remaining three fields are enabled and there default selected values will gets passed to the table.

I think my last 2 lines make sense!

The only thing to cosider is

1. which checkbox has been checked and which is the textfield, radio,radio, textarea names of that selected checkbox and how to capture and insert into DB.

2. if multiple then how

0
 
gdemariaCommented:
Please see my post above which shows the action of the form (how to insert/udpate the database)

Does it make sense?   Do you have any questions about it?   What do you need next?

0
 
Gurpreet Singh RandhawaWeb DeveloperAuthor Commented:
where i find

<cfloop index="ii" from="1" to="#form.totalRecords#">

total records, how i know which one checked counts to total records, how i handle disabled values,

these above are my questions..

:(

0
 
gdemariaCommented:
> how i know which one checked counts to total records

when you draw your form, you count your records (variable "O" in your example)

Then store that value in a hidden field in your form, that is your TotalRecords  form field.  That will tell your action how many times to loop.

> how i handle disabled values

Disabled values will not appear in your form post's action, so all you have to do is test for the form value to be sure it exists, if it doesn't, just skip it...

<cfloop index="ii" from="1" to="#form.totalRecords#">

   <cfif structKeyExists(form, "CheckValue#kk#")>
        ... do everything...
       <cfset variables.firstName = form['firstName' & kk]>
        ... insert/udpate...
   <cfelse>
        Skipping disabled row....
   </cfif>

</cfloop>

0
 
Gurpreet Singh RandhawaWeb DeveloperAuthor Commented:
you used "ii" in outloop and using kk inside loop

why so?
0
 
Gurpreet Singh RandhawaWeb DeveloperAuthor Commented:
if disabled does not exists, do not just skip just add no to radio and empty to textarea so aleast some value must go
0
 
gdemariaCommented:

> you used "ii" in outloop and using kk inside loop,   why so?

Type-o, use one or the other

> do not just skip just add no to radio and empty to textarea so aleast some value must go

Ok, you can change the CFIF condition so that you enter those values
0
 
Gurpreet Singh RandhawaWeb DeveloperAuthor Commented:
you mean either i should ii or i should kk. is that what you are saying
0
 
gdemariaCommented:
> you mean either i should ii or i should kk. is that what you are saying

are you seriously asking me which loop variable you should use?  

I like to use double characters such as kk,  ii or  xx.   Sometimes with big loops you have to do a "find" to locate or change your variable and that's impossible if you use just "i" or just "n" because it matches everything.

But pick your favorite loop variable and use it for the entire loop, it doesn't matter which one you use!


0
 
Gurpreet Singh RandhawaWeb DeveloperAuthor Commented:
I am acceping your solution but it was not the way as it was explained by you,

I have to pass each form field details in a list and then do a list append for form variable, then use it on next page using splits and then use the evaluate function to find out which checkbox was checked and pick its value and usage, but still thanks for a uick response & guidance you gave

Appreciate
0
 
gdemariaCommented:
>  I am acceping your solution but it was not the way as it was explained by you,

The problem is trying to figure out what you have and what you need.   Your questions would be much easier if you would simply post your code and explain what you need it to do.   Instead we just have to keep guessing and guessing and guessing, your responses are vague and confusing.   You need to post your actual Coldfusion code from now on.


>  I have to pass each form field details in a list and then do a list append for form variable, then use it on next page using splits and then use the evaluate function to find out which checkbox was checked and pick its value and usage


Just like this...  this may make sense to you, but not to anyone else.   How can anyone translate this into code?   It's not possible.    From now on show your code.

>  then use the evaluate function

You should never have to use the evaluate function.   Use the structure reference for much faster processing

Form["myvariable" & kk]



0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 13
  • 12
Tackle projects and never again get stuck behind a technical roadblock.
Join Now