Solved

Checkbox problem

Posted on 2006-10-20
12
183 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
[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
  • 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
Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

 
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 200 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

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

Suggested Solutions

Title # Comments Views Activity
Configure IIS to process JSON 10 103
Domino Website - Redirection 12 120
setup wamp server for first time 2 113
SonicWall port forward 4 17
Have you ever sent email via ColdFusion and thought of tracking this mail to capture the exact date and time when the message was opened ?  If yes, then this article is for you ! First we need a table user_email with columns user_id , email , sub…
Introduction This article explores the design of a cache system that can improve the performance of a web site or web application.  The assumption is that the web site has many more “read” operations than “write” operations (this is commonly the ca…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

738 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