?
Solved

Checkbox problem

Posted on 2006-10-20
12
Medium Priority
?
190 Views
Last Modified: 2013-12-24
Hello!

I have couple of images with checkbox. There have 5 images and user can choose his favourite image, then user press submit button and that checkbox data go to the action page. But now the problem is that action page insert all pictures to the database (not that which is user favourite image)

Here is my inset page code
<cfquery name="getdetails" datasource="#dsn#">
select imgid, picture, text
from pictures
</cfquery>
<html>
<head>
<title>pictures</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<form action="laheta2_action.cfm" method="post">
<table width="500">
<cfloop query="getdetails">


          <td>
          <cfoutput><font face="Arial, Helvetica, sans-serif" size="2"><img src="#dir##getdetails.picture#" border=0 width=100 alt="">
            <input name="picture" type="checkbox" value="#imgid#">
            <INPUT type="hidden" name="nimi" value="#kuva#">
            <br>
         
            <br>
            </font></cfoutput> </td>
      
      
      </tr>
       <cfset tc=0>
      </cfloop>  </table>
 <input type="submit" name="Submit" value="Submit">
</form>
</body>
</html>


...and here is my action page code (where the magic happens, I want that only those pictures goes to the database which user have chosen using checkbox)

<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<cfif isDefined("form.picture")>
 <cfloop list="#form.picture#" index="i">
   <cfquery name="syota" datasource="#dsn#">
INSERT INTO            kilpi      
                        (imgid, picture)
VALUES
                        ('#imgid#', '#form.picture#')
   </cfquery>
 </cfloop>
</cfif>



</body>
</html>

0
Comment
Question by:mattimeikalainen
  • 6
  • 6
12 Comments
 
LVL 20

Expert Comment

by:trailblazzyr55
ID: 17774290
Change to this and see if it helps...


Change Image to this...

<img src="#dir##picture#" border=0 width=100 alt="">

Change checkbox to this....

<input name="picture" type="checkbox" value="#imgid#|#picture#">




Change if statement on action page to this...

<cfif isDefined("form.picture")>
   <cfloop list="#form.picture#" index="i">
      <cfquery name="syota" datasource="#dsn#">
         INSERT INTO kilpi (imgid, picture)
         VALUES ("#listfirst(i,'|')#", "#listlast(i,'|')#")
      </cfquery>
   </cfloop>
</cfif>
0
 
LVL 20

Expert Comment

by:trailblazzyr55
ID: 17774390
when you loop over a list ie:

as you have here....

 <cfloop list="#form.picture#" index="i">
   <cfquery name="syota" datasource="#dsn#">
       INSERT INTO  kilpi (imgid, picture)
       VALUES ('#imgid#', '#form.picture#')
   </cfquery>
 </cfloop>

the index "i" is the actual list item for each iteration through the loop...
so you can see why each time you looped, you weren't even referencing the list, you just had variables in there...

so lets say you have a list like so...

Try this simple example to see what I mean......

<cfset myList = "Honda, Ford, Buick, Chevy">
<cfoutput>
  <cfloop list="#myList#" index="myCar">
   #myCar#<br>
  </cfloop>
</cfoutput>

See how "myCar" is actually the item in the list? So that's why we give your check box the value "ImageId"|"Picture"

because with each one selected... you'll form a list like so...  image001|picture001,image003|picture003

then you loop over that list wit say the index of "TheItem"

which will give you each item...

1)   image001|picture001
2)   image003|picture003

then listfirst(TheItem,'|') will give you "image001"
and listlast(TheItem,'|') will give you "picture001"

it's like a list within a list...
0
 

Author Comment

by:mattimeikalainen
ID: 17775312
Sorry, that not work. It still insert all pictures to the database.
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 20

Expert Comment

by:trailblazzyr55
ID: 17775509
can you repost your code with the changes you made?
0
 

Author Comment

by:mattimeikalainen
ID: 17775679
this send page:

<cfloop query="getdetails">


          <td>
          <cfoutput><font face="Arial, Helvetica, sans-serif" size="2"><img src="#dir##kuva#" border=0 width=100 alt="">
            <input name="kuvat" type="checkbox" value="#imgid#|#picture#">
            <INPUT type="hidden" name="nimi" value="#picture#">


and this is action page:

<cfif isDefined("form.picture")>
   <cfloop list="#form.picture#" index="i">
      <cfquery name="syota" datasource="#dsn#">
         INSERT INTO kilpi (imgid, picture)
         VALUES ("#listfirst(i,'|')#", "#listlast(i,'|')#")
      </cfquery>
   </cfloop>
</cfif>
0
 
LVL 20

Expert Comment

by:trailblazzyr55
ID: 17775791
in looking at this, you changed your checkbox name to "kuvat", but you didn't change the "form.pictures" name in the action page to match that value

try this on your action page...

<cfif isDefined("form.kuvat")> <!--- this has to be the form name of your checkbox --->
   <cfloop list="#form.kuvat#" index="i"> <!--- this has to be the form name of your checkbox --->
      <cfquery name="syota" datasource="#dsn#">
         INSERT INTO kilpi (imgid, picture)
         VALUES ("#listfirst(i,'|')#", "#listlast(i,'|')#")
      </cfquery>
   </cfloop>
</cfif>
0
 

Author Comment

by:mattimeikalainen
ID: 17776220
I chance my database and cfm code little bit. Here is the new ones
 input page:
<form action="laheta2_action.cfm" method="post">
<table width="500">
<cfloop query="getdetails">


          <td>
          <cfoutput><font face="Arial, Helvetica, sans-serif" size="2"><img src="#dir##getdetails.kuva#" border=0 width=100 alt="">
            <input name="kuva" type="checkbox" value="#imgid#|#kuva#">
            <INPUT type="hidden" name="nimi" value="#kuva#">
            <br>
         
            <br>
            </font></cfoutput> </td>
      
      
      </tr>
       <cfset tc=0>
      </cfloop>

action page:

<cfif isDefined("form.kuvat")>
   <cfloop list="#form.kuvat#" index="i">
      <cfquery name="syota" datasource="#dsn#">
         INSERT INTO kilpi (tunnus, kuva)
         VALUES ('#tilausnumero#', "#listlast(i,'|')#")
      </cfquery>
   </cfloop>
</cfif>



but now i get next error message  [Microsoft][ODBC Microsoft Access Driver] Invalid bracketing of name '[kayntikortti3.jpg]'.
0
 
LVL 20

Expert Comment

by:trailblazzyr55
ID: 17776511
change it to this instead.... you "have" to make sure the form you're looping over in the action page has the same name as the checkbox....

<form action="laheta2_action.cfm" method="post">
<table width="500">
<cfloop query="getdetails">


         <td>
          <cfoutput><font face="Arial, Helvetica, sans-serif" size="2"><img src="#dir##getdetails.kuva#" border=0 width=100 alt="">
            <input name="kuva" type="checkbox" value="#getdetails.kuva#">
            <INPUT type="hidden" name="nimi" value="#getdetails.kuva#">
            <br>
         
            <br>
            </font></cfoutput> </td>
     
     
     </tr>
      <cfset tc=0>
     </cfloop>

action page:

<cfif isDefined("form.kuva")>
   <cfloop list="#form.kuva#" index="i">
      <cfquery name="syota" datasource="#dsn#">
         INSERT INTO kilpi (tunnus, kuva)
         VALUES ('#tilausnumero#', "#i#") <!--- also note here --->
      </cfquery>
   </cfloop>
</cfif>




I'm not sure what the variable "tilausnumero" is, but if it doesn't need to be specific to the item selected, you're fine... otherwise stick to what we did before with the "imgid"
0
 

Author Comment

by:mattimeikalainen
ID: 17779157
"tilausnumero" =(ordernumber)
is this:

<cfset theList = "1234567890">
<cfset tilausnumero = "">
<cfloop from="1" to="8" index="idx">
  <cfset tilausnumero = tilausnumero & mid(theList, randRange(1,len(theList)), 1)>
</cfloop>
0
 

Author Comment

by:mattimeikalainen
ID: 17779240
We are so close, now i change code little bit and now data goes to the database. But only and big problem is that when I choose example 3 pictures, that data goes three times in to database. Here is link to my print screen picture from my database.

http://www.realex.fi/coldfusion/database.jpg

and here is codes
send page:

<form action="laheta2_action.cfm" method="post">
<table width="500">
<cfloop query="getdetails">
          <td>
          <cfoutput><font face="Arial, Helvetica, sans-serif" size="2">
<img src="#dir##getdetails.kuva#" border=0 width=100 alt="">
<input name="kuva" type="checkbox" value="#getdetails.kuva#">
            <br>
            <br>
            </font></cfoutput> </td>      
      </tr>
       <cfset tc=0>
      </cfloop

and action page:

<cfset theList = "1234567890">
<cfset tilausnumero = "">
<cfloop from="1" to="8" index="idx">
  <cfset tilausnumero = tilausnumero & mid(theList, randRange(1,len(theList)), 1)>
</cfloop>
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<cfif isDefined("form.kuva")>
   <cfloop list="#form.kuva#" index="i">
      <cfquery name="syota" datasource="#dsn#">
         INSERT INTO kilpi (tunnus, kuvat)
         VALUES ('#tilausnumero#','#form.kuva#')
      </cfquery>
   </cfloop>
</cfif>
</body>
</html>
0
 
LVL 20

Accepted Solution

by:
trailblazzyr55 earned 800 total points
ID: 17780612
change your last statement on your action page to this..... and it'll fix that problem


<cfif isDefined("form.kuva")>
   <cfloop list="#form.kuva#" index="myImg">
      <cfquery name="syota" datasource="#dsn#">
         INSERT INTO kilpi (tunnus, kuvat)
         VALUES ('#tilausnumero#','#myImg#')
      </cfquery>
   </cfloop>
</cfif>

When looping over a list like above, your use the index variable to get the item in the list to insert...

~trail
0
 

Author Comment

by:mattimeikalainen
ID: 17783455
Thank you very much!
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

This is a guide to setting up a new WHM/cPanel Server to be used for web hosting accounts. It is intended for web hosting company administrators and dedicated server owners. For under $99 per month (considering normal rate of Big Data Cetnters like …
One of the typical problems I have experienced is when you have to move a web server from one hosting site to another. You normally prepare all on the new host, transfer the site, change DNS and cross your fingers hoping all will be ok on new server…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses
Course of the Month14 days, 19 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